Matplotlib vline label parameter not showing

风流意气都作罢 提交于 2019-12-12 08:47:36

问题


I want to label my vertical lines with matplotlib's .vline command, but for some reason the label parameter doesn't do anything/show anything on the final plot. Does anyone know how to get the label to show?

plt.vlines(x=pah, ymin=0, ymax=0.6, colors='0.75', linestyles='dashed', label='PAHs')

Everything works apart from the label.

Many thanks,

L


回答1:


The label keyword is displayed in the legend. You need create the legend explicitly to see the label in the plot:

plt.vlines([1,2,3], 0, 1, label='test')
plt.legend()



回答2:


For text near your line as in this example use:

vline_value = 3

fig, ax = plt.subplots(figsize=(10,10))
ax.axvline(x=vline_value, ymin=0, ymax=1) 
x_bounds = ax.get_xlim()
ax.annotate(s='vline_value', xy =(((vline_value-x_bounds[0])/(x_bounds[1]-x_bounds[0])),1.01), xycoords='axes fraction', verticalalignment='right', horizontalalignment='right bottom' , rotation = 270)
fig.savefig('example')

Also, this short script holds more options if you want: https://pythonhosted.org/lineid_plot/#




回答3:


This works

plt.plot(x,y)
plt.vlines(x=pah, ymin=0, ymax=0.6, colors='0.75', linestyles='dashed', label='PAHs')
plt.legend()

but I don't know if this is what you expect



来源:https://stackoverflow.com/questions/27154793/matplotlib-vline-label-parameter-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!