图中Python自定义标题和图例

图中Python自定义标题和图例,python,matplotlib,Python,Matplotlib,我试图在我的身材顶部显示一个图例(如),并在上面显示一个标题(如) 但是,当我保存此图表时,标题和图例都将丢失。只有轴和数据点保存在pdf中。您正在将它们移出可见区域。如果您使用set\u titlempl会为您注意它的位置。与图例上的边界框相同: import numpy as np import matplotlib.pyplot as plt x = np.arange(1,24,1) y = np.array([400,650,1020,1300,1600,1950,2200,2550

我试图在我的身材顶部显示一个图例(如),并在上面显示一个标题(如)


但是,当我保存此图表时,标题和图例都将丢失。只有轴和数据点保存在pdf中。

您正在将它们移出可见区域。如果您使用
set\u title
mpl会为您注意它的位置。与图例上的边界框相同:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,24,1)
y = np.array([400,650,1020,1300,1600,1950,2200,2550,2850,3150,3400,3550,3800,3950,4050,4150,4210,4250,4300,4320,4310,4300,4200])

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(x,y,'o', label='data set 1')
ax.set_title('Data set A-1')

ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_ylim(ymax=5000)

ax.legend(loc='upper center', 
          ncol=3, fancybox=True, shadow=True)
#plt.tight_layout()
plt.show()


您可能还希望将
num_points=1
作为一个标记传递给
图例

您正在将它们移出可见区域。如果您使用
set\u title
mpl会为您注意它的位置。与图例上的边界框相同:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,24,1)
y = np.array([400,650,1020,1300,1600,1950,2200,2550,2850,3150,3400,3550,3800,3950,4050,4150,4210,4250,4300,4320,4310,4300,4200])

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(x,y,'o', label='data set 1')
ax.set_title('Data set A-1')

ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_ylim(ymax=5000)

ax.legend(loc='upper center', 
          ncol=3, fancybox=True, shadow=True)
#plt.tight_layout()
plt.show()


您可能还希望将
num_points=1
作为kwarg传递到
图例

您希望图例也位于轴上吗

#!/usr/bin/python3

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,24,1)
y = np.array([400,650,1020,1300,1600,1950,2200,2550,
              2850,3150,3400,3550,3800,3950,4050,4150,
              4210,4250,4300,4320,4310,4300,4200])

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(x,y,'o', label='data set 1')

ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_ylim(ymax=5000)


ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
          ncol=3, fancybox=True, shadow=True)
title = ax.set_title('Data set A-1')

fig.tight_layout()

fig.subplots_adjust(top=0.85)
title.set_y(1.07)

fig.savefig("13.png")


是否希望图例也位于轴上

#!/usr/bin/python3

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,24,1)
y = np.array([400,650,1020,1300,1600,1950,2200,2550,
              2850,3150,3400,3550,3800,3950,4050,4150,
              4210,4250,4300,4320,4310,4300,4200])

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(x,y,'o', label='data set 1')

ax.set_xlabel('x', fontsize=14)
ax.set_ylabel('y', fontsize=14)
ax.set_ylim(ymax=5000)


ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
          ncol=3, fancybox=True, shadow=True)
title = ax.set_title('Data set A-1')

fig.tight_layout()

fig.subplots_adjust(top=0.85)
title.set_y(1.07)

fig.savefig("13.png")


您使用的是哪一个版本的Python?Python 2.7.7、Spyder 2.3.0(但与Spyder无关)它们太高了,只是不在图中的视图中。尝试将图形的大小调整为较小,它们开始显示…如果将1.4更改为1.1,将1.25更改为1.05,您将看到它们。@Agean test.pdf中的标题(水平)切成两半。但是如果1.4->1.05和1.25->1.05和
fontsize=14
,那么就没关系了……您使用的是哪一版本的Python?Python 2.7.7、Spyder 2.3.0(但与Spyder无关)它们太高了,只是不在图的视图中。尝试将图形的大小调整为较小,它们开始显示…如果将1.4更改为1.1,将1.25更改为1.05,您将看到它们。@Agean test.pdf中的标题(水平)切成两半。但是如果1.4->1.05和1.25->1.05和
fontsize=14
,那么就可以了。。。