简体   繁体   English

如何在下面的情节中添加图例?

[英]How to add legend to below plot?

Trying to add legend to line plot but not able to do it.试图将图例添加到线图但无法做到。 Pls suggest some methods or solutions.请建议一些方法或解决方案。

counts, bin_edges = np.histogram(df_status_1['nodes'], bins=10, 
density = True)
pdf = counts/(sum(counts))
print(pdf);
print(bin_edges);
cdf = np.cumsum(pdf)
plt.plot(bin_edges[1:],pdf);
plt.plot(bin_edges[1:], cdf)

counts, bin_edges = np.histogram(df_status_2['nodes'], bins=10, 
density = True)
pdf = counts/(sum(counts))
print(pdf);
print(bin_edges);
cdf = np.cumsum(pdf)
plt.xlabel('nodes');
plt.ylabel('values');
plt.title('CDF of long & short surviving ppl');
plt.plot(bin_edges[1:],pdf);
plt.plot(bin_edges[1:], cdf)

Tried to add plt.legend() but getting "No handles with labels found to put in legend" error.enter code here试图添加 plt.legend() 但得到“没有找到放置在图例中的标签的句柄”错误。在此处输入代码

Store each plt.plot to an axis with coma(plt.plot returns tuple)将每个 plt.plot 存储到一个带有 coma 的轴(plt.plot 返回元组)

ex:前任:

arr1, = plt.plot(bin_edges[1:],pdf);
arr2, = plt.plot(bin_edges[1:], cdf)
plt.legend([arr1,arr2], ['PDF survived','CDF survived']) 

try like above.像上面一样尝试。

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

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