繁体   English   中英

Matplotlib:如何相应地填充以下具有所有 x 轴标签和网格线的图表?

[英]Matplotlib : How to populate the below chart having all the x-axis labels and grid lines accordingly?

在此处输入图片说明

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 轴标签。 请建议。

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()

tplotlib

这是在评论中的讨论之后。

有几个部分,完整的例子在底部。

风格

你的问题之一是如何改变情节的风格。 这可以通过以下代码完成:

import matplotlib.pyplot as plt 
plt.style.use('seaborn-darkgrid')

有许多可能的样式,如果您愿意,您甚至可以创建自己的样式。 要查看所有可能的样式,请参阅: 文档 要列出所有样式,请使用plt.style.available

自定义代码

对于自定义代码:您可以使用FixedLocator或者如果您知道它是 log 或 symlog,那么matplotlib有一个内置的定位器。 有关比例,请参阅matplotlib 文档

您可以使用 FixedLocator 来设置轴,以进行分离。 即下面的代码会给你你想要的。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

X = np.arange(0, 2000)
Y = np.arange(0, 2000)

def convert(date): 
    if 'w' in date: 
        return 7*int(date[:-1])
    if 'm' in date: 
        return 30*int(date[:-1]) 
    if 'y' in date: 
        return 30*int(date[:-1]) + 360   
ticks = [convertdate(d) for d in tenor]
plt.style.use('seaborn-darkgrid') 
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))

plt.plot(X, Y, c = 'k')
plt.show()

其中产生: 在此处输入图片说明

具体案例

对于您的特定情况,您可能希望自定义代码处于特定的时间间隔(即 rate_t 的最小值,rate_t 的最大值)。

因此,您需要convert函数更改为如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

x = data['rate_t']
y = data['rate_s']

def get_indices(date): 
    if 'w' in date: 
        return 7*int(date[:-1])
    if 'm' in date: 
        return 30*int(date[:-1]) 
    if 'y' in date: 
        return 30*int(date[:-1]) + 360   

def convert(indices): 
    x = np.linspace(min(data['rate_t']), max(data['rate_t']), indices[-1] + 1)
    return x[indices]
indices = [get_indices(d) for d in tenor]
ticks = convert(indices)
plt.style.use('seaborn-darkgrid') 
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))

plt.plot(x, y, c = 'k')
plt.show()

(假设data['rate_s']data['rate_t']原样且未经处理)

这会产生这个: 在此处输入图片说明

如果您有任何问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM