我在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()
你能告诉我我该怎么做吗?我会很感激每条评论,感谢您的帮助。
对我来说,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()
输出:
与没有:
感谢您的回答Quang。它也对我有用。也许是后续问题。当我做机器人想显示y-Ticks时,有没有办法清除y轴上的小水平线?
是否可以删除y轴标记(但不能删除名称)?