2016-11-24 36 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