使横坐标的字体变小
时间: 2024-02-25 18:56:55 浏览: 49
你可以在 `plt.setp()` 方法中设置横坐标刻度标签的字体大小。例如:
```
import matplotlib.pyplot as plt
from matplotlib.dates import WeekdayLocator, DayLocator, DateFormatter, date2num
from mpl_finance import candlestick_ohlc
# 定义绘制 K 线图的函数
def pandas_candlestick_ohlc(stock_data, otherseries=None, figsize=(12, 6)):
# 设置绘图参数,主要是坐标轴
mondays = WeekdayLocator(MONDAY)
alldays = DayLocator()
dayFormatter = DateFormatter('%d')
fig, ax = plt.subplots(figsize=figsize)
fig.subplots_adjust(bottom=0.2)
if stock_data.index[-1] - stock_data.index[0] < pd.Timedelta('730 days'):
weekFormatter = DateFormatter('%b %d')
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
else:
weekFormatter = DateFormatter('%b %d, %Y')
ax.xaxis.set_major_formatter(weekFormatter)
ax.grid(True)
# 创建 K 线图
stock_array = np.array(stock_data.reset_index()[['date','open','high','low','close']])
stock_array[:,0] = date2num(stock_array[:,0])
candlestick_ohlc(ax, stock_array, colorup = "red", colordown="green", width=0.6)
plt.title('厦门象屿', fontsize='9')
# 可同时绘制其他折线图
if otherseries is not None:
for each in otherseries:
plt.plot(stock_data[each], label=each)
plt.legend()
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right', fontsize=8) # 设置字体大小为 8
plt.savefig('E:/应统案例大赛/附件1-股票交易数据/k线图a4.jpg')
plt.show()
stock_data.index.name = 'date' # 日期为索引列
# 对股票数据的列名重新命名
stock_data = stock_data[['open', 'high', 'low', 'close', 'chengjiaoe', 'zongshizhi', 'huanshoulv', 'shiyinglv', 'shijinglv']]
data = stock_data.loc['2022-11-03':'2022-12-14'] # 获取某个时间段内的时间序列数据
pandas_candlestick_ohlc(data, figsize=(8, 4))
```
在这里,我们在 `plt.setp()` 方法中设置了 `fontsize` 参数为 8,用于控制横坐标刻度标签的字体大小。你可以根据需要调整 `fontsize` 的值。
阅读全文