2016-11-24 7 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') 

は、どのように私は、行のために字幕を追加することができます?。私はPythonでプロットに追加することができますどのように、フォトショップで「タイトル1」と「タイトル2」を作っ

enter image description here

:それは次のようになりますか?

+0

あなたは '各サブプロットにタイトルを与えるために、しかしこれは、行の途中に表示されません' ax3.set_title(「ここに挿入タイトル」)を使用することができます.... – DavidG

答えて

2

set_titleを呼び出し、matplotlibのはpyplot.suptitleを持っています。 Figureごとにsuptitleが1つしかないことがありますので、2行の図を表示したい場合は問題は解決しません。 plt.text() 1こちらも欲しかっされていない軸にテキストを設定することができますので、私はその後plt.subplots_adjust(hspace = 0.3)

を使用して間の間隔にサブプロットの行を調整するために必要な場合があり plt.figtext

を使用することをお勧めを使用し

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

これはかなり簡単です。サブプロットとプロットのタイトルを作るためにサブプロット

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

それは私が望むものではありません。真ん中に現れている上の2つのプロットと下の2つのプロットを組み合わせたタイトルが必要です。 – spore234

関連する問題