温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Changing figure size does not work in Matplotlib
matplotlib python

python - 更改图形大小在Matplotlib中不起作用

发布于 2021-01-05 11:48:52

我在Matplotlib中有一个图,我想通过增加图表的宽度来增加x轴刻度线之间的边距。我想使用这里提到的代码

plt.figure(figsize=(20,10)) ->似乎没有任何作用

fig, ax = plt.subplots(figsize=(20, 10)) ->创建一个带有空图的图表

这是我的代码:

from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
load = [0.0, 0.1, 0.5, 0.7, 0.4, 0.5, 0.4, 0.3, 0.4, 0.5, 0.65, 0.75, 0.8, 0.7, 0.25, 0.1, 0.1, 0.75, 0.2, 0.0, 0.0, 0.0, 0.5, 0.65, 0.5]
hours = list(range(25)) # [0, 1, 2, ... 22, 23, 24]
labels = [f'{h:02d}:00' for h in hours] # ["00:00", "01:00", ... "23:00", "24:00"]


f = interp1d(hours, load)
f2 = interp1d(hours, load, kind='cubic')

xnew = np.linspace(0, 24, num=500, endpoint=True)




plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
plt.xticks(np.arange(25), labels)  # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90, fontsize=13)
plt.xlim(xmin=0.0)

plt.plot(xnew, f2(xnew), color="gold", linewidth=3)
plt.legend(['Generation'], loc='best')

plt.legend( ['Electricity generation'], bbox_to_anchor=(0.5, 1.2), loc='upper center' , fontsize=16)
plt.ylabel("Electrical power", fontsize=18, labelpad=8)
plt.xlabel("Time of day", fontsize=18, labelpad=8)
plt.xlim(xmin=0.0)
plt.tick_params(labelleft=False)

plt.savefig('Generation_2.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

你能告诉我我该怎么做吗?我会很感激每条评论,感谢您的帮助。

查看更多

提问者
VanessaF
被浏览
44
Quang Hoang 2020-09-01 04:46

对我来说,plt.figsize()在执行任何plot命令之前:

plt.figure(figsize=(20,10))

# all other plot command    
plt.xticks(np.arange(0, 25, step=1))  # Set label locations.
plt.xticks(np.arange(25), labels)  # Set text labels.
plt.xticks(np.arange(25), labels, rotation=90, fontsize=13)
plt.xlim(xmin=0.0)

plt.plot(xnew, f2(xnew), color="gold", linewidth=3)
plt.legend(['Generation'], loc='best')

plt.legend( ['Electricity generation'], bbox_to_anchor=(0.5, 1.2), loc='upper center' , fontsize=16)
plt.ylabel("Electrical power", fontsize=18, labelpad=8)
plt.xlabel("Time of day", fontsize=18, labelpad=8)
plt.xlim(xmin=0.0)
plt.tick_params(labelleft=False)

plt.savefig('Generation_2.png', edgecolor='black', dpi=400, bbox_inches='tight')
plt.show()

输出:

在此处输入图片说明

与没有:

在此处输入图片说明