我有这个代码,我想在图片右侧中间的两张图片中为这两张图片添加一个图例。我的意思是说两张照片现在都是正确的,传说将在右侧,并且对于两个图形和其中的8条线是相同的。有人知道怎么做吗?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator
from matplotlib.transforms import BlendedGenericTransform
X = np.linspace(0, 50, 256, endpoint=True)
y1 = np.cos((np.pi*X)/25)
y2 = -np.cos((np.pi*X)/25)
y3 = np.sin((np.pi*X)/25)
y4 = -np.sin((np.pi*X)/25)
#y5 = np.cos((np.pi*X)/25)
#y6 = -np.cos((np.pi*X)/25)
#y7 = np.sin((np.pi*X)/25)
#y8 = -np.sin((np.pi*X)/25)
ax = plt.subplot(2, 1, 1)
a=plt.plot(X, y1,color="green", linewidth=3.0, label=r'$\cos(\frac{\pi x}{25})$')
b=plt.plot(X, y2,color="cyan", linewidth=3.0, label=r'$\-cos(\frac{\pi x}{25})$')
c=plt.plot(X, y3,color="blue", linewidth=3.0, label=r'$\sin(\frac{\pi x}{25})$')
d=plt.plot(X, y4,color="red", linewidth=3.0, label=r'$\-sin(\frac{\pi x}{25})$')
plt.xticks([0, np.pi*4, np.pi*8, np.pi*12, np.pi*16],
[r'$0$', r'$4\pi$', r'$8\pi$', r'$12\pi$', r'$16\pi$'])
plt.yticks([-1, -0.5, 0, 0.5, +1],
[r'$-1$', r'$-\frac{1}{2}$', r'$0$', r'$\frac{1}{2}$', r'$+1$'])
ax.xaxis.set_minor_locator(MultipleLocator(np.pi))
plt.title('Set default color cycle to rgby')
bx = plt.subplot(2, 1, 2)
e=plt.plot(X, y1,color="magenta", linewidth=3.0, label=r'$\cos(\frac{\pi x}{25})$')
f=plt.plot(X, y2,color="black", linewidth=3.0, label=r'$-\cos(\frac{\pi x}{25})$')
g=plt.plot(X, y3,color="cyan", linewidth=3.0, label=r'$\sin(\frac{\pi x}{25})$')
h=plt.plot(X, y4,color="yellow", linewidth=3.0, label=r'$-\sin(\frac{\pi x}{25})$')
plt.xticks([0, np.pi*4, np.pi*8, np.pi*12, np.pi*16],
[r'$0$', r'$4\pi$', r'$8\pi$', r'$12\pi$', r'$16\pi$'])
plt.yticks([-1, -0.5, 0, 0.5, +1],
[r'$-1$', r'$-\frac{1}{2}$', r'$0$', r'$\frac{1}{2}$', r'$+1$'])
bx.xaxis.set_minor_locator(MultipleLocator(np.pi))
plt.title('Set axes color cycle to cmyk')
plt.subplots_adjust(hspace=0.4)
plt.figlegend((a,b,c,d,e,f,g,h), (r'$\cos(\frac{\pi x}{25})$',r'$\-cos(\frac{\pi x}{25})$', r'$\sin(\frac{\pi x}{25})$', r'$\-sin(\frac{\pi x}{25})$', r'$\cos(\frac{\pi x}{25})$' , r'$-\cos(\frac{\pi x}{25})$' , r'$\sin(\frac{\pi x}{25})$' , r'$-\sin(\frac{\pi x}{25})$'), loc=(0.85, 0.65))
plt.show()
非常感谢你!
答案 0 :(得分:1)
我稍微改变了你的代码,因为我个人更喜欢用数字和轴明确地工作。请参阅plt.subplots(x,y)调用。对于图例处理程序,您只是错过了添加逗号。参见例如here。此外,你错放了figlegend调用中的两个减号。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator
X = np.linspace(0, 50, 256, endpoint=True)
y1 = np.cos((np.pi*X)/25)
y2 = -np.cos((np.pi*X)/25)
y3 = np.sin((np.pi*X)/25)
y4 = -np.sin((np.pi*X)/25)
fig, [ax,bx] = plt.subplots(2,1)
a, = ax.plot(X, y1,color="green", linewidth=3.0, label="A")
b, = ax.plot(X, y2,color="cyan", linewidth=3.0, label="B")
c, = ax.plot(X, y3,color="blue", linewidth=3.0, label="C")
d, = ax.plot(X, y4,color="red", linewidth=3.0, label="D")
ax.set_xticks([0, np.pi*4, np.pi*8, np.pi*12, np.pi*16],
[r'$0$', r'$4\pi$', r'$8\pi$', r'$12\pi$', r'$16\pi$'])
ax.set_yticks([-1, -0.5, 0, 0.5, +1],
[r'$-1$', r'$-\frac{1}{2}$', r'$0$', r'$\frac{1}{2}$', r'$+1$'])
ax.xaxis.set_minor_locator(MultipleLocator(np.pi))
ax.set_title('Set default color cycle to rgby')
e, = bx.plot(X, y1,color="magenta", linewidth=3.0, label="E")
f, = bx.plot(X, y2,color="black", linewidth=3.0, label="F")
g, = bx.plot(X, y3,color="cyan", linewidth=3.0, label="G")
h, = bx.plot(X, y4,color="yellow", linewidth=3.0, label="H")
bx.set_xticks([0, np.pi*4, np.pi*8, np.pi*12, np.pi*16],
[r'$0$', r'$4\pi$', r'$8\pi$', r'$12\pi$', r'$16\pi$'])
bx.set_yticks([-1, -0.5, 0, 0.5, +1],
[r'$-1$', r'$-\frac{1}{2}$', r'$0$', r'$\frac{1}{2}$', r'$+1$'])
bx.xaxis.set_minor_locator(MultipleLocator(np.pi))
bx.set_title('Set axes color cycle to cmyk')
fig.subplots_adjust(hspace=0.4)
plt.figlegend((a,b,c,d,e,f,g,h), (r'$\cos(\frac{\pi x}{25})$',r'$-\cos(\frac{\pi x}{25})$', r'$\sin(\frac{\pi x}{25})$', r'$-\sin(\frac{\pi x}{25})$', r'$\cos(\frac{\pi x}{25})$' , r'$-\cos(\frac{\pi x}{25})$' , r'$\sin(\frac{\pi x}{25})$' , r'$-\sin(\frac{\pi x}{25})$'), loc=(0.8, 0.1))
fig.show()
我不确定这是否是最好的方法,但它似乎更接近你所要求的。现在你必须使用轴和文本的大小和位置来使它适合你。