Plot rates with dropdown lines in R
By : Sunil Chaliya
Date : March 29 2020, 07:55 AM
this will help If you want to see how rates cluster (or not) at each time slice, I think you'd do better to use points rather than vertical lines, ideally with transparent colors so you can see where they overlap. Here's one way to do that in base R, using a toy version of your data set with five y series: code :
# Make a toy data set with five y series
set.seed(1)
df < data.frame(time = c(paste(rep(1, 15), seq(45, 59, 1), sep=":"), paste0(rep(2, 10), ":0", seq(0, 9)), paste(rep(2, 5), seq(10, 15), sep=":")),
y1 = rnorm(31, 1, 0.1), y2 = rnorm(31, 1, 0.1), y3 = rnorm(31, 1, 0.1), y4 = rnorm(31, 1, 0.1), y5 = rnorm(31, 1, 0.1), stringsAsFactors=FALSE)
# Make a sequence along your time series to use as your x values; as the previous
# answer said, you can't use raw strings as x values for plotting.
df$x < seq_along(df$time)
# Now plot them. Start by making an empty plot space that covers the full range
# of y values in your data set; then make a better xaxis; then plot the points, using
# alpha() from 'scales' to make the points transparent so overlapping ones show up.
with(df, plot(x = x, y = y1, type = "n", xaxt="n", pch=20, bg=alpha("black", 0.5), col=alpha("black", 0.5),
ylab = "y value", xlab = "time", ylim = c(min(df[,2:6]), max(df[,2:6]))))
axis(1, at=seq(length(df$x)), labels=df$time, tick=FALSE, las=2)
for (i in 1:5) points(x = df$x, y = df[,paste0("y", i)], pch=20, col=alpha("black", 0.5))

Solve best fit polynomial and plot dropdown lines
By : Diego Zambrana
Date : March 29 2020, 07:55 AM
Hope this helps I'm using R 3.3.1 (64bit) on Windows 10. I have an xy dataset that I've fit with a 2nd order polynomial. I'd like to solve that bestfit polynomial for x at y=4, and plot dropdown lines from y=4 to the xaxis. , You can use the quadratic formula to calculate the values: code :
betas < coef(fit2) # get coefficients
betas[1] < betas[1]  4 # adjust intercept to look for values where y = 4
# note degree increases, so betas[1] is c, etc.
betas
## (Intercept) poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)2
## 8.7555833 6.0807302 0.7319848
solns < c((betas[2] + sqrt(betas[2]^2  4 * betas[3] * betas[1])) / (2 * betas[3]),
(betas[2]  sqrt(betas[2]^2  4 * betas[3] * betas[1])) / (2 * betas[3]))
solns
## poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)1
## 1.853398 6.453783
segments(solns, 1, solns, 4, col = 'green') # add segments to graph
polyroot(betas)
## [1] 1.853398+0i 6.453783+0i

plot dataframe column on one axis and other columns as separate lines on the same plot (in different color)
By : Shmulik Almani
Date : March 29 2020, 07:55 AM
it should still fix some issue I have following dataframe. , Remove column before ploting: code :
df.drop('cutoff', axis=1).plot()
df = df.set_index(df['cutoff'])
df.drop('cutoff', axis=1).plot()
df = df.set_index('cutoff')
df.plot()

How can I drop repeating values in a column while keeping the data for its rows?
By : user3648276
Date : March 29 2020, 07:55 AM
I hope this helps you . If want replace duplicated values to empty strings use Series.duplicated with Series.mask: code :
df['Name'] = df['Name'].mask(df['Name'].duplicated(), '')
print (df)
Name Data1 Data2
0 A 0.1 1.1
1 0.2 1.2
2 0.3 1.3
3 B 0.6 1.6
4 0.7 1.7
5 0.8 1.8
6 C 1.0 2.0
7 1.1 2.1
8 1.2 2.2
df1 = df.set_index(['Name','Data1'])
print (df1)
Data2
Name Data1
A 0.1 1.1
0.2 1.2
0.3 1.3
B 0.6 1.6
0.7 1.7
0.8 1.8
C 1.0 2.0
1.1 2.1
1.2 2.2
with pd.option_context('display.multi_sparse', False):
print (df1)
Data2
Name Data1
A 0.1 1.1
A 0.2 1.2
A 0.3 1.3
B 0.6 1.6
B 0.7 1.7
B 0.8 1.8
C 1.0 2.0
C 1.1 2.1
C 1.2 2.2
print (df1.index.tolist())
[('A', 0.1), ('A', 0.2), ('A', 0.3),
('B', 0.6), ('B', 0.7), ('B', 0.8),
('C', 1.0), ('C', 1.1), ('C', 1.2)]

Multiple lines on line plot/time series with matplotlib or plot.ly on Python
By : Julien
Date : March 29 2020, 07:55 AM
seems to work fine Assuming your data is in a pandas dataframe df, it would be hard to plot it without the groups being in separate columns, but that is actually a step very easily done in one line, code :
df.pivot(index="Date", columns="Group", values="Value").plot()
u = u"""Date Group Value
1/01/2015 A 50
2/01/2015 A 60
1/01/2015 B 100
2/01/2015 B 120
1/01/2015 C 40
2/01/2015 C 55
1/01/2015 D 36
2/01/2015 D 20"""
import io
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(io.StringIO(u), delim_whitespace=True)
df["Date"] = pd.to_datetime(df["Date"])
df.pivot(index="Date", columns="Group", values="Value").plot()
plt.show()

