gpt4 book ai didi

python - Matplotlib xy 线图图例

转载 作者:行者123 更新时间:2023-12-01 05:22:32 25 4
gpt4 key购买 nike

如何在 IPython 笔记本内的 Matplotlib 中向 xy 线图添加图例?我目前的尝试:

x = np.linspace(0, 3*np.pi, 500)
a = plt.plot(x, np.sin(x**2))
b = plt.plot(x, np.sin(x**0.5))
plt.legend([a,b], ['square', 'square root'])

这样做时,我收到以下错误:

/Users/mc/.virtualenvs/kaggle/lib/python2.7/site-packages/matplotlib/legend.py:613: UserWarning: Legend does not support [] Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

(str(orig_handle),)) /Users/mc/.virtualenvs/kaggle/lib/python2.7/site-packages/matplotlib/legend.py:613: UserWarning: Legend does not support [] Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

(str(orig_handle),))

如果我执行 plt.scatter 而不是 plt.plot,则此命令有效,但我想要一个折线图而不是 x,y 点。

最佳答案

这样做怎么样?

x = np.linspace(0, 3*np.pi, 500)
fig, ax = plt.subplots()
a = ax.plot(x, np.sin(x**2), label = 'square')
b = ax.plot(x, np.sin(x**0.5), label = 'square root')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels)

要得到这个:

enter image description here

关于python - Matplotlib xy 线图图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22006041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com