サブプロットで図のサイズを変更するにはどうすればいいですか? 質問する

サブプロットで図のサイズを変更するにはどうすればいいですか? 質問する

図のサイズを大きくするにはどうすればいいですか?この図?

これは何もしません:

f.figsize(15, 15)

リンクからのサンプルコード:

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

plt.close('all')

# Just a figure and one subplot
f, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[0].set_title('Sharing X axis')
axarr[1].scatter(x, y)

# Two subplots, unpack the axes array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)

# Three subplots sharing both x/y axes
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing both axes')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)

# row and column sharing
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

# Four polar axes
f, axarr = plt.subplots(2, 2, subplot_kw=dict(projection='polar'))
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
# Fine-tune figure; make subplots farther from each other.
f.subplots_adjust(hspace=0.3)

plt.show()

ベストアンサー1

使用.set_figwidthそして.set_figheightmatplotlib.figure.Figure返されるオブジェクトplt.subplots()、または両方を設定するf.set_size_inches(w, h)

f.set_figheight(15)
f.set_figwidth(15)

注:set_size_inches()では関数名に測定単位が明示的に記載されていますが、 と ではインチを使用するため、このことは当てはまりませんset_figwidth()。このset_figheight()情報は、関数のドキュメントで提供されています。

あるいは、 を使用して.subplots()新しい図を作成する場合は、次のように指定しますfigsize=

f, axs = plt.subplots(2, 2, figsize=(15, 15))

.subplots受け入れ**fig_kw、渡されるpyplot.figure、そしてfigsizeが見つかる場所です。

図のサイズを設定するとValueError例外が発生する可能性があります:

Image size of 240000x180000 pixels is too large. It must be less than 2^16 in each direction

これは、関数がインチではなくピクセルで動作することを前提としているため、関数を使用する際によく発生する問題ですset_fig*()(明らかに、240000*180000 インチは大きすぎます)。

おすすめ記事