2016-11-24 254 views
1

行对于这样一个情节:添加字幕的情节

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True) 
ax1.plot(x, y) 
ax2.scatter(x, y) 
ax3.scatter(x, 2 * y ** 2 - 1, color='r') 
ax4.plot(x, 2 * y ** 2 - 1, color='r') 

如何添加字幕行?它应该是这样的:

enter image description here

我做“标题1”和“标题2”用Photoshop,我怎能把它们添加到python中的情节?

+0

你可以用'ax3.set_title(“插入标题下面”)'给一个标题到每个插曲,但是这并没有出现在该行的中间.... – DavidG

回答

2

为了使标题大选的一个情节,matplotlib有pyplot.suptitle。由于每个图只能有一个suptitle,如果您想要两行数字,它不能解决问题。

使用plt.text()一个可以设置文字的轴,也不会在这里想要的,所以我会建议使用plt.figtext

它可能然后使用需要调整spacingbetween次要情节的行plt.subplots_adjust(hspace = 0.3)

import matplotlib.pyplot as plt 
import numpy as np 

x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True) 
ax1.plot(x, y) 
ax2.scatter(x, y) 
ax3.scatter(x, 2 * y ** 2 - 1, color='r') 
ax4.plot(x, 2 * y ** 2 - 1, color='r') 

plt.figtext(0.5,0.95, "A tremendously long title that wouldn't fit above a single figure", ha="center", va="top", fontsize=14, color="r") 
plt.figtext(0.5,0.5, "Yet another multi-worded title that needs some space", ha="center", va="top", fontsize=14, color="r") 
plt.subplots_adjust(hspace = 0.3) 
plt.savefig(__file__+".png") 
plt.show() 

enter image description here

+0

谢谢,我希望有一个更优雅的解决方案,但这很方便。 – spore234

-1

这样做相当简单;调用set_title的次要情节

import matplotlib.pyplot as plt 
import numpy as np 

plt.style.use('ggplot') 
x = np.linspace(0, 2 * np.pi, 400) 
y = np.sin(x ** 2) 

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True) 
ax1.plot(x, y) 
ax1.set_title("a blue line") 

ax2.scatter(x, y) 
ax2.set_title("cool blue dots") 

ax3.scatter(x, 2 * y ** 2 - 1, color='r') 
ax3.set_title("cool red dots") 

ax4.plot(x, 2 * y ** 2 - 1, color='r') 
ax4.set_title("a red line") 

plt.show() 

with titles]

+0

这不是我想要的。我想要一个上面两个组合的标题和中间出现的较低的两个标题。 – spore234