I have a time series plot and I would like to add a vertical line to it at event time. If I use this code:
event_time = pd.to_datetime('10/12/2016 06:21:00')
ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none',...
color = 'black', marker = 'o')
ax1 = ax.twinx()
ax1.axvline(event_time, color='red', linestyle='-')
df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none', ...
color = 'green', marker = 'o')
It takes a subset of the time series starting at event_time and doesn't produce a vertical line.
If I move ax1.axvline(event_time, color='red', linestyle='-') to the bottom, I get the plot I want but the vertical line is still missing.
event_time = pd.to_datetime('10/12/2016 06:21:00')
ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none',...
color = 'black', marker = 'o')
ax1 = ax.twinx()
df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none',...
color = 'green', marker = 'o')
ax1.axvline(event_time, color='red', linestyle='-')
How can I get the vertical line to discplay at x = event_time for all y values?
works with plt
ax = df_stats_t.plot(x = 't', y='t_TI_var_pwr', linestyle='none', color = 'black', marker = 'o')
ax1 = ax.twinx()
df_stats_t.plot(x='t',y='t_TI_var_ws',ax=ax1, linestyle='none', color = 'green', marker = 'o')
plt.axvline(event_time, color='red', linestyle='-')
Related
The first image is the figure I'm trying to reproduce, and the second image is the data I have. Does anyone have a clean way to do this with pandas or matplotlib?
Just transpose the DataFrame and use df.plot with the stacked flag set to true:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.DataFrame({'squad': [0.6616, 0.1245, 0.0950],
'quac': [0.83, 0.065, 0.0176],
'quoref': [0.504, 0.340364, 0.1067]})
# Transpose
plot_df = df.T
# plot
ax = plot_df.plot(kind='bar', stacked=True, rot='horizontal')
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0.)
ax.set_ylabel("% of Questions")
plt.tight_layout()
plt.show()
You can try this:
data = {'squad':[0.661669, 0.127516, 0.095005],
'quac':[0.930514, 0.065951, 0.017680],
'quoref': [0.504963, 0.340364, 0.106700]}
df = pd.DataFrame(data)
bars_1 = df.iloc[0]
bars_2 = df.iloc[1]
bars_3 = df.iloc[2]
# Heights of bars_1 + bars_2
bars_1_to_2 = np.add(bars_1, bars_2).tolist()
# The position of the bars on the x-axis
r = [0, 1, 2]
plt.figure(figsize = (7, 7))
plt.bar(r, bars_1, color = 'lightgrey', edgecolor = 'white')
plt.bar(r, bars_2, bottom = bars_1, color = 'darkgrey', edgecolor = 'white')
plt.bar(r, bars_3, bottom = bars_1_to_2, color = 'dimgrey', edgecolor = 'white')
plt.yticks(np.arange(0, 1.1, 0.1))
plt.xticks(ticks = r, labels = df.columns)
plt.ylabel('% of Questions')
plt.show()
I receive the error No handles with labels found to put in legend. when running the code below. How can I add a legend to this scatter plot that shows the color definitions (a red dot for A, blue dot for B, green dot for C)?
### Dummy Dataset
x = [0,1,-1,4,0,2,2,4,2]
y = [1,5,9,2,4,2,5,6,1]
cat = ['A','B','B','B','A','C','A','B','B']
df = pd.DataFrame(list(zip(x,y,cat)), columns =['x', 'y', 'cat'])
### Build color definitions
df.loc[:, 'color'] = df.cat
df.color.replace(['A', 'B', 'C'], ['red', 'blue', 'green'], inplace=True)
display(df)
### Plotting
fig = plt.figure(figsize=(5,5), constrained_layout=True)
gs = fig.add_gridspec(2, 1)
ax1 = fig.add_subplot(gs[0, 0])
ax1.scatter(df.x, df.y, edgecolors = 'none', c = df.color)
ax1.legend(loc='upper left', facecolor='white', frameon=1,
framealpha=1, labelspacing=0.2, borderpad=0.25)
It seems like there might not be a way to do this without a simple loop. Based on the procedure here, the following code works.
x = [0,1,-1,4,0,2,2,4,2]
y = [1,5,9,2,4,2,5,6,1]
cat = ['A','B','B','B','A','C','A','B','B']
df = pd.DataFrame(list(zip(x,y,cat)), columns =['x', 'y', 'cat'])
mycolorsdict = {'A':'red', 'B':'blue', 'C':'green'}
fig = plt.figure(figsize=(5,5), constrained_layout=True)
gs = fig.add_gridspec(2, 1)
ax1 = fig.add_subplot(gs[0, 0])
grouped = df.groupby('cat')
for key, group in grouped:
group.plot(ax=ax1, kind='scatter',
x='x', y='y',
label=key, color=mycolorsdict[key])
ax1.legend(loc='upper left', facecolor='white', frameon=1,
framealpha=1, labelspacing=0.2, borderpad=0.25)
I am plotting a segmentation, and running into an issue where my categorical values are only showing up one color. Any advice?
[![fig = plt.figure(figsize = (20,10))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Sentiment Score', fontsize = 15)
ax.set_ylabel('Star Review', fontsize = 15)
ax.set_title('Heinz Segmentation', fontsize = 20)
targets = \['Sugar/ Healthy Positives', 'Sugar/ Healthy Negatives', 'Price/ Value Positives', 'Price/ Value Negatives', 'Purists Positives', 'Purists Negatives'\]
colors = \['r', 'g', 'b', 'c', 'm', 'y'\]
for target, color in zip(targets,colors):
ax.scatter(df.Compound,df.StarsInt
, c = color
, s = 200
,alpha = .5)
ax.legend(targets, prop={'size': 14})
ax.grid()][1]][1]
IIUC, you have a problem in the legends. You forgot to pass the legend while plotting
for target, color in zip(targets,colors):
ax.scatter(df.Compound,df.StarsInt, c=color,
s = 200,alpha = .5,
label=target) # <--- pass the label to show in legend
I'm trying to reproduce this diagram:
but I'm having trouble creating the horizontal lines with bars. I've tried annotate and hlines but they don't quite give the effect I'm after.
import matplotlib.pyplot as plt
plt.grid(which = 'both')
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(-0.5,8)
plt.ylim(-0.5,10)
plt.xlabel('Redshift, z', fontsize = 16)
plt.hlines(8, 0, .3)
plt.annotate(r'H$\alpha$', fontsize = 16, xy = (0,8), xycoords='data', xytext=(0,8),
textcoords='data',
arrowprops=dict(arrowstyle='<|-|>', connectionstyle='arc3', color = 'k', lw=2))
fig = plt.gcf()
width, height = 15,35 # inches
fig.set_size_inches(width, height, forward = True)
plt.show()
What's the best way to produce the bars like this?
I would use annotate directly, but for more flexibility, I would separate the drawing of the horizontal bars and the corresponding text
plt.figure()
plt.grid(which = 'both')
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(-0.5,8)
plt.ylim(-0.5,10)
plt.xlabel('Redshift, z', fontsize = 16)
bar_ys = [8,4]
bar_xs = [[0,6],[3,5]]
bar_texts = [r'H$\alpha$',r'H$\beta$']
bar_color = ['k','orange']
for y,xs,t,c in zip(bar_ys,bar_xs,bar_texts,bar_color):
plt.annotate('', xy = (xs[0],y), xycoords='data', xytext=(xs[1],y),
arrowprops=dict(arrowstyle='|-|', color=c, lw=2, shrinkA=0, shrinkB=0))
plt.annotate(t, xy = (xs[1],y), xycoords='data', xytext=(-5,5), textcoords='offset points',
fontsize = 16, va='baseline', ha='right', color=c)
plt.show()
The accepted answer works perfectly, thank you.
In addition, I automated the colours thus:
colors = iter(cm.tab10(np.linspace(0,0.8,13)))
colour = 'k'
for y,xs,t in zip(bar_ys,bar_xs,bar_texts):
plt.annotate('', xy = (xs[0],y), xycoords='data', xytext=(xs[1],y),
arrowprops=dict(arrowstyle='|-|', color=colour, lw=2, shrinkA=0, shrinkB=0))
plt.annotate(t, xy = (xs[1],y), xycoords='data', xytext=(-5,5), textcoords='offset points',
fontsize = 16, va='baseline', ha='right', color=colour)
colour = next(colors)
I am having 'similar' issues to Matplotlib, legend with multiple different markers with one label. I was able to achieve the following thanks to this question Combine two Pyplot patches for legend.
fig = pylab.figure()
figlegend = pylab.figure(figsize=(3,2))
ax = fig.add_subplot(111)
point1 = ax.scatter(range(3), range(1,4), 250, marker=ur'$\u2640$', label = 'S', edgecolor = 'green')
point2 = ax.scatter(range(3), range(2,5), 250, marker=ur'$\u2640$', label = 'I', edgecolor = 'red')
point3 = ax.scatter(range(1,4), range(3), 250, marker=ur'$\u2642$', label = 'S', edgecolor = 'green')
point4 = ax.scatter(range(2,5), range(3), 250, marker=ur'$\u2642$', label = 'I', edgecolor = 'red')
figlegend.legend(((point1, point3), (point2, point4)), ('S','I'), 'center', scatterpoints = 1, handlelength = 1)
figlegend.show()
pylab.show()
However, my two (venus and mars) markers overlap in the legend. I tried playing with handlelength, but that doesn't seem to help. Any suggestions or comments would be helpful.
A possible workaround is to create a two column legend with blank labels in the first column:
figlegend.legend((point1, point2, point3, point4), (' ', ' ', 'S', 'I'),
'center', scatterpoints = 1, ncol = 2)
Here's my work-around MWE. I actually plot two extra "plots", point_g and point_r which have the legend handles we will use. I then cover them up by using a white squre marker. Plot the remaining plots as desired.
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text', **{'latex.preamble': '\\usepackage{wasysym}'})
plt.rc('lines', **{'markersize':20})
fig = plt.figure()
point_g, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='g')
point_r, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='r')
plt.plot((0,), (0,), marker='s', mec='w', mfc='w')
plt.plot(range(3), range(1,4), ls='none', marker='$\\male$', mec='g')
plt.plot(range(3), range(2,5), ls='none', marker='$\\male$', mec='r')
plt.plot(range(1,4), range(3), ls='none', marker='$\\female$', mec='g')
plt.plot(range(2,5), range(3), ls='none', marker='$\\female$', mec='r')
plt.axis([-0.1, 4.1, -0.1, 4.1])
plt.legend((point_g, point_r), ('Green', 'Red'), markerscale=1.6, numpoints=1,
borderpad=0.8, handlelength=3, labelspacing=1)
plt.show()
Note: You do not need the LaTeX preamble if you use unicode symbols. I couldn't get them working on my system (Linux) so I used the LaTeX symbols. This method will work with all symbols, just remove the plt.rc commands and change \\male and \\female to your unicode characters.