简体   繁体   English

如何在我的情节中获得传奇?

[英]How can I get a legend in my plot?

How can I get a legend from the Elev_Avg component of polydatx ?我怎样才能从一个传说Elev_Avg的成分polydatx

I tried using ax.legend() , but I got this error:我尝试使用ax.legend() ,但出现此错误:

No handles with labels found to put in legend.

Plot情节

f, ax = plt.subplots(1, figsize=(8, 8))
polydatx.plot(ax = ax, column = 'Elev_Avg', cmap='OrRd', scheme='quantiles', label='elev')
segdatx.plot(ax = ax)
ax.grid(False)
ax.legend()

You have to tell the computer what to put in the legend.你必须告诉计算机在图例中放什么。 Then you will get it :)然后你会得到它:)

For example, if what you are plotting are two columns ['s1', 's2'] from a pandas dataframe df, then this is what you would do to get the plot and legend:例如,如果您要绘制的是来自 Pandas 数据框 df 的两列 ['s1', 's2'],那么您将执行以下操作来获取绘图和图例:

ax.plot(df.loc[:,['s1', 's2']], marker='o')
ax.legend(['s1', 's2'])
plt.show()

According matplotlib document ( matplotlib ) the handles is optional, but for some reason most people get that error message.根据 matplotlib 文档( matplotlib ),句柄是可选的,但出于某种原因,大多数人都会收到该错误消息。 What I did was我所做的是

handles, labels = ax.get_leggend_handles_labels()句柄,标签 = ax.get_leggend_handles_labels()

ax.legend(handles, labels). ax.legend(手柄,标签)。 It seems to work for me.它似乎对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM