Xlabel 字体大小不会在绘图中改变
Xlabel fontsize don't change in plot
我正在尝试在不同的绘图中绘制一些数据,我能够做到,但是,x 轴的标签大小在第一个绘图中没有改变,但在第二个绘图中改变了.这就是我正在做的事情:
import matplotlib.pyplot as plt
from dateutil.relativedelta import relativedelta as rd
from calendar import monthrange
fin_mes = date.today() - rd(days=1)
# Start ploting data A
fig, ax = plt.subplots(1, 1)
# Retrieve dates
mes = fin_mes.strftime("%B")
anio = fin_mes.strftime("%Y")
# Set title
plt.suptitle(f"Data {mes} - {anio}")
# List of days of the last month
num_days = range(1, int(monthrange(fin_mes.day, fin_mes.month)[1]) + 1)
print(num_days)
# Set name from x label
ax.set_xlabel('Dates')
# Set name from y label
ax.set_ylabel('Data')
# Set name from the plot to save
name_a = f"DataA-{mes}-{anio}.png"
plt.title("My Data")
plt.xlim(0, num_days[-1])
plt.ylim((min(num_days)),
(max(num_days)))
plt.xticks(num_days)
# Setting the size to the label
plt.rc('xtick', labelsize=5)
plt.grid(True)
dataa_plot, = plt.plot(num_days, num_days, label="DATA A")
plt.legend(handles=[dataa_plot])
plt.show()
fig.savefig(name_a)
# Start ploting data B
fig, ax = plt.subplots(1, 1)
# Retrieve dates
mes = fin_mes.strftime("%B")
anio = fin_mes.strftime("%Y")
# Set title
plt.suptitle(f"Data {mes} - {anio}")
# List of days of the last month
num_days = range(1, int(monthrange(fin_mes.day, fin_mes.month)[1]) + 1)
print(num_days)
# Set name from x label
ax.set_xlabel('Dates')
# Set name from y label
ax.set_ylabel('Data')
# Set name from the plot to save
name_b = f"DataB-{mes}-{anio}.png"
plt.title("My Data B")
plt.xlim(0, num_days[-1])
plt.ylim((min(num_days)),
(max(num_days)))
plt.xticks(num_days)
# Setting the size to the label
plt.rc('xtick', labelsize=5)
plt.grid(True)
datab_plot, = plt.plot(num_days, num_days, label="DATA B")
plt.legend(handles=[datab_plot])
plt.show()
fig.savefig(name_b)
这样我就得到了这个图:
图b的字体大小改变了,但图A没有。我不知道为什么会这样。希望有人能帮帮我,谢谢。
PD:我在 Lubuntu x64 中使用 python 3.8.10,matplotlib=3.5.1
你必须设置标签尺寸before使用它来设置刻度,即plt.rc('xtick', labelsize=5)
必须在plt.xticks(num_days)
之前生效(最安全的方式就是把它移到绘图的最开始。
作为(更简单的)替代方法,您可以直接在 xticks
中设置字体大小,而无需更改 rc 参数:
plt.xticks(num_days, fontsize=5)
我正在尝试在不同的绘图中绘制一些数据,我能够做到,但是,x 轴的标签大小在第一个绘图中没有改变,但在第二个绘图中改变了.这就是我正在做的事情:
import matplotlib.pyplot as plt
from dateutil.relativedelta import relativedelta as rd
from calendar import monthrange
fin_mes = date.today() - rd(days=1)
# Start ploting data A
fig, ax = plt.subplots(1, 1)
# Retrieve dates
mes = fin_mes.strftime("%B")
anio = fin_mes.strftime("%Y")
# Set title
plt.suptitle(f"Data {mes} - {anio}")
# List of days of the last month
num_days = range(1, int(monthrange(fin_mes.day, fin_mes.month)[1]) + 1)
print(num_days)
# Set name from x label
ax.set_xlabel('Dates')
# Set name from y label
ax.set_ylabel('Data')
# Set name from the plot to save
name_a = f"DataA-{mes}-{anio}.png"
plt.title("My Data")
plt.xlim(0, num_days[-1])
plt.ylim((min(num_days)),
(max(num_days)))
plt.xticks(num_days)
# Setting the size to the label
plt.rc('xtick', labelsize=5)
plt.grid(True)
dataa_plot, = plt.plot(num_days, num_days, label="DATA A")
plt.legend(handles=[dataa_plot])
plt.show()
fig.savefig(name_a)
# Start ploting data B
fig, ax = plt.subplots(1, 1)
# Retrieve dates
mes = fin_mes.strftime("%B")
anio = fin_mes.strftime("%Y")
# Set title
plt.suptitle(f"Data {mes} - {anio}")
# List of days of the last month
num_days = range(1, int(monthrange(fin_mes.day, fin_mes.month)[1]) + 1)
print(num_days)
# Set name from x label
ax.set_xlabel('Dates')
# Set name from y label
ax.set_ylabel('Data')
# Set name from the plot to save
name_b = f"DataB-{mes}-{anio}.png"
plt.title("My Data B")
plt.xlim(0, num_days[-1])
plt.ylim((min(num_days)),
(max(num_days)))
plt.xticks(num_days)
# Setting the size to the label
plt.rc('xtick', labelsize=5)
plt.grid(True)
datab_plot, = plt.plot(num_days, num_days, label="DATA B")
plt.legend(handles=[datab_plot])
plt.show()
fig.savefig(name_b)
这样我就得到了这个图:
图b的字体大小改变了,但图A没有。我不知道为什么会这样。希望有人能帮帮我,谢谢。
PD:我在 Lubuntu x64 中使用 python 3.8.10,matplotlib=3.5.1
你必须设置标签尺寸before使用它来设置刻度,即plt.rc('xtick', labelsize=5)
必须在plt.xticks(num_days)
之前生效(最安全的方式就是把它移到绘图的最开始。
作为(更简单的)替代方法,您可以直接在 xticks
中设置字体大小,而无需更改 rc 参数:
plt.xticks(num_days, fontsize=5)