Matplotlib:如何生成下面的图表,其中包含所有x轴标签和相应的网格线? [英] Matplotlib : How to produce the below chart containing all x-axis labels and gridlines accordingly?

查看:126
本文介绍了Matplotlib:如何生成下面的图表,其中包含所有x轴标签和相应的网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data = {'tenor': ['1w','1m','3m','6m','12m','1y','2y','3y','4y','5y','6y','7y','10y','15y','20y','25y','30y','40y','50y'],'rate_s': [0.02514, 0.026285, 0.0273, 0.0279, 0.029616, 0.026526, 0.026028, 0.024, 0.025958,0.0261375, 0.026355, 0.026, 0.026898, 0.0271745, 0.02741, 0.027, 0.0275, 0.0289,0.0284],'rate_t':[ 0.02314, 0.024285, 0.0253,0.0279, 0.028616, 0.026526,0.027028, 0.024, 0.025958,0.0271375, 0.02355, 0.026, 0.024898, 0.0271745, 0.02641,0.027, 0.0255, 0.0289,0.0284]}

我想生成具有以下相同格式的图表.我尝试了这段代码,但结果并不令人满意.它还没有显示所有x轴标签.请提出建议.

I want to produce the chart with the same format like below. I tried this piece of code but results are not satisfactory. It also not showing all x-axis labels. Please suggest.

ax = plt.gca()

df.plot(kind='line',x='tenor',y='rate_s',marker='o',color='green',ax=ax)
df.plot(kind='line',x='tenor',y='rate_y',marker='o', color='red', ax=ax)
ax.minorticks_on()
ax.grid(which='major',linestyle='-', linewidth='0.5', color='blue')
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()

推荐答案

我将时间段转换为timedelta并绘制:

I would convert the time periods into timedelta and plot:

df['periods'] = pd.to_timedelta(df.tenor).dt.total_seconds()

fig, ax = plt.subplots(figsize=(12,6))
df.plot(x='periods',y=['rate_s','rate_t'], ax=ax)
ax.set_xticks(df.periods);
ax.set_xticklabels(df.tenor)
plt.grid()
plt.show()

输出:

这篇关于Matplotlib:如何生成下面的图表,其中包含所有x轴标签和相应的网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆