-3

我想在 matplotlib 中使图形的 x 轴更宽,我使用以下代码。但似乎 figsize 没有任何效果。如何更改图形的大小?

data_dates = np.loadtxt(file,usecols = 0, dtype=np.str)
data1 = np.loadtxt(file,usecols = 1)
data2 = np.loadtxt(file,usecols = 2)
data3 = np.loadtxt(file,usecols = 3)

plt.plot(figsize=(30,5))
plt.plot(data_dates,data1, label = "T")
plt.plot(data_dates,data2, label = "WS")
plt.plot(data_dates,data3, label = "WD")
plt.xlabel('Date', fontsize=8)
plt.xticks(rotation=90,fontsize=4)
plt.ylabel(' Percentage Difference (%)')
plt.legend()
plt.savefig(outfile,format='png',dpi=200,bbox_inches='tight')

该文件的样本是

01/06/2019  0.1897540512577196  0.28956205456965856 0.10983099750547703
02/06/2019  0.1914523564094276  0.1815325705314345  0.0004533827128655877
03/06/2019  0.2365346386184113  0.12301344973593868 0.058843355966174876
04/06/2019  0.2085897993039386  0.005466902564359565    0.014087537281676313
05/06/2019  0.15563355684612554 0.16249844426472368 0.11036007669758358
06/06/2019  0.11981475728282368 0.11015459703126898 0.03501167308950372
4

1 回答 1

2
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(30, 5)

plt.plot(data_dates,data1, label = "T")
plt.plot(data_dates,data2, label = "WS")
plt.plot(data_dates,data3, label = "WD")

plt.xlabel('Date', fontsize=8)
plt.xticks(rotation=90,fontsize=4)
plt.ylabel(' Percentage Difference (%)')
plt.legend()
plt.savefig("test.png",format='png',dpi=200,bbox_inches='tight')

除了使用子图显式创建图形之外,您还可以使用 get-current-figure 方法fig = plt.gcf()

于 2021-09-08T07:49:50.323 回答