軸の制限を設定する方法 質問する

軸の制限を設定する方法 質問する

matplotlib の y 軸の制限を設定するのに助けが必要です。以下は私が試したコードですが、うまくいきませんでした。

import matplotlib.pyplot as plt

plt.figure(1, figsize = (8.5,11))
plt.suptitle('plot title')
ax = []
aPlot = plt.subplot(321, axisbg = 'w', title = "Year 1")
ax.append(aPlot)
plt.plot(paramValues,plotDataPrice[0], color = '#340B8C', 
     marker = 'o', ms = 5, mfc = '#EB1717')
plt.xticks(paramValues)
plt.ylabel('Average Price')
plt.xlabel('Mark-up')
plt.grid(True)
plt.ylim((25,250))

このプロットのデータでは、y 軸の制限は 20 と 200 になります。ただし、制限は 20 と 250 にしたいです。

ベストアンサー1

を介して現在の軸を取得しplt.gca()、その制限を設定します。

ax = plt.gca()
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])

おすすめ記事