制作Python粉丝图表/粉丝图 [英] Making a Python Fan Chart / Fan Plot
问题描述
我正在寻找在Python中制作类似于来填充阴影区域.
下面的代码是一个玩具示例,可以对一个简单的二次函数执行此操作.它遍历 vals
,并将它们添加到原始信号 y
中,并在它们之间进行填充.对于 vals
中的每个 val
,它都会修改 alpha
参数.它还绘制信号 y
本身,这与您的BoE图表不同,您可以通过注释掉该行来删除它.
正如我所说,这是一个玩具例子.您必须自己弄清楚如何在数据中使用它,但是希望它能证明它可以完成.
将matplotlib.pyplot导入为plt将numpy导入为npN = 1000x = np.linspace(0,10,N)y = x ** 2一个= np.ones(N)vals = [30,20,10]#迭代和从y中减去的值.无花果,ax = plt.subplots()对于我来说,枚举(vals)中的val:alpha = 0.5 *(i + 1)/len(vals)#修改每次迭代的alpha值.ax.fill_between(x,y + ones * val,y-ones * val,color ='red',alpha = alpha)ax.plot(x,y,color ='red')#绘制原始信号plt.show()
I'm looking to make a fan chart type line plot in Python which resembles the Bank of England Interest Rate fan charts like this one:
I'm quite well practiced with using matplotlib for standard line/bar/scatter plots, however this seems like it would require something of a more custom implementation.
I've Google'd about and cannot seem to find any standard libraries for Python that do this sort of thing at all or even any code that describes how this might be done.
Any help on how this could be achieved would be really appreciated.
You can do this using matplotlib.pyplot.fillbetween
to fill in the shaded areas.
The code below is a toy example that does this for a simple quadratic. It iterates over vals
and adds these to your original signal y
and fills in between them. For each val
in vals
it modifies the alpha
argument. It also plots the signal y
itself, which is different to how your BoE chart does it, you can remove this if you wish by commenting out the line.
As I said, this is a toy example. You'll have to figure out how to use this with your data yourself, but hopefully it demonstrates that it can be done.
import matplotlib.pyplot as plt
import numpy as np
N = 1000
x = np.linspace(0, 10, N)
y = x**2
ones = np.ones(N)
vals = [30, 20, 10] # Values to iterate over and add/subtract from y.
fig, ax = plt.subplots()
for i, val in enumerate(vals):
alpha = 0.5*(i+1)/len(vals) # Modify the alpha value for each iteration.
ax.fill_between(x, y+ones*val, y-ones*val, color='red', alpha=alpha)
ax.plot(x, y, color='red') # Plot the original signal
plt.show()
这篇关于制作Python粉丝图表/粉丝图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!