ax.fill_between(years_to_plot, -680- 220, -680 + 220, alpha=0.2,color='yellow',label='Brun et al.2017')如何调整label字体大小
时间: 2023-06-20 16:10:16 浏览: 165
你可以在该行代码之前加入以下代码来调整字体大小:
```
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 12}) # 修改字体大小为12
```
这将会把字体大小设置为12,你也可以根据需要将其更改为其他大小。如果你想要更改所有标签的字体大小,而不是只是这个标签,那么你可以省略 `label` 参数并在 `ax.legend()` 函数之前添加以下代码:
```
ax.tick_params(labelsize=12)
```
这会将轴标签和刻度标签的字体大小设置为12。
相关问题
import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator # 创建画布和子图对象 fig, ax = plt.subplots(figsize=(9, 6), dpi=100) # 绘制折线图 ax.plot(x, y) # 绘制平均值线 #ax.axhline(y=-650, color='r', linestyle='--',label='流域整体物质平衡=-650mm w.e.') # 添加阴影带 start_year = 2006 end_year = 2016 mask = np.logical_and(years >= start_year, years <= end_year) years_to_plot = years[mask] ax.fill_between(years_to_plot, -680- 220, -680 + 220, alpha=0.2,color='yellow',label='Brun et al.2017') ax.axhline(-680, color='yellow', linestyle='--',xmin=0.65, xmax=0.89) start_year_2 = 2000 end_year_2 = 2014 mask_2 = np.logical_and(years >= start_year_2, years <= end_year_2) years_to_plot_2 = years[mask_2] ax.fill_between(years_to_plot_2, -790-110, -790+110, alpha=0.2, color='green',label='Wu et al.2018') ax.axhline(-790, color='green', linestyle='--',xmin=0.51, xmax=0.840) start_year_3 = 2000 end_year_3 = 2018 mask_3 = np.logical_and(years >= start_year_3, years <= end_year_3) years_to_plot_3 = years[mask_3] ax.fill_between(years_to_plot_3, -540-160, -540+160, alpha=0.2, color='blue',label='Shean et al.2020') ax.axhline(-540, color='blue', linestyle='--',xmin=0.51, xmax=0.93) start_year_4 = 2000 end_year_4 = 2019 mask_4 = np.logical_and(years >= start_year_4, years <= end_year_4) years_to_plot_4 = years[mask_4] ax.fill_between(years_to_plot_4, -580-220, -580+220, alpha=0.2, color='red',label='Hugonnet et al.2021') ax.axhline(-580, color='red', linestyle='--',xmin=0.51, xmax=0.957) # 设置 x 轴标签和标题 ax.set_xlabel('年份',fontproperties=font_prop,fontsize=14) ax.set_ylabel('物质平衡(mm w.e.)',fontproperties=font_prop,fontsize=14) ax.set_title('图8 帕隆藏布流域1980-2019物质平衡',fontproperties=font_prop,fontsize=14,y=-0.17) # 强制显示整数刻度 ax.xaxis.set_major_locator(MaxNLocator(integer=True)) # 添加网格 ax.grid(True, which='major', linestyle='--') # 将坐标轴的刻度字体大小设置为12 ax.tick_params(axis='both', which='major', labelsize=12) # 添加图例 ax.legend(fontsize=24,loc='lower left',prop=font_prop) # 设置图形的边距 plt.tight_layout() # 显示图形 plt.show()
该代码是用Python中的matplotlib库绘制折线图,并添加了四个阴影带,代表不同研究者的数据范围。其中,ax.plot(x, y)用于绘制折线图,ax.fill_between(years_to_plot, -680- 220, -680 + 220, alpha=0.2,color='yellow',label='Brun et al.2017')用于添加阴影带,ax.axhline(-680, color='yellow', linestyle='--',xmin=0.65, xmax=0.89)用于绘制平均值线。其他部分则是对图形进行美化,如添加图例、设置坐标轴标签和标题等。最后使用plt.show()将图形显示出来。
fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') ax.set_title('Prediction Intervals') ax.set_xlabel('Time') ax.set_ylabel('RV_5min') plt.legend() plt.show() fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') fig, ax = plt.subplots() ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual') ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间') ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间') 怎么将两个ax.fill_between画在一幅图上,不相互遮挡,
你可以使用相同的 `ax` 对象来绘制多个 `fill_between` 图形,以确保它们在同一幅图上不会相互遮挡。下面是一个示例代码:
```python
fig, ax = plt.subplots()
ax.plot(RV_5.index, RV_5['RV_5min'], color='black', label='Actual')
ax.fill_between(RV_5.index, RV_5['RV_5min'] - forecast_error, RV_5['RV_5min'] + forecast_error, alpha=0.2, color='blue', label='无ps置信区间')
ax.fill_between(RV_5.index, conf_int_lower, conf_int_upper, alpha=0.2, color='green', label='有ps置信区间')
ax.set_title('Prediction Intervals')
ax.set_xlabel('Time')
ax.set_ylabel('RV_5min')
plt.legend()
plt.show()
```
这段代码将在同一幅图中绘制 `Actual` 曲线,并在其上方分别绘制了 `无ps置信区间` 和 `有ps置信区间` 的填充区域,它们不会相互遮挡。注意确保只创建一个 `fig` 和 `ax` 对象,并在同一个 `ax` 对象上调用多个 `fill_between` 方法。
阅读全文