如何创建堆积线图?
我希望能够生成堆积线图(类似于使用的方法 这里)使用Python(最好使用matplotlib,但另一个库也可以)。我该怎么做?
这类似于他们网站上的 堆叠条形图示例,除了我就像条形的顶部用线段连接以及下面的区域要填充一样。我也许可以通过减少条形之间的间隙并使用大量条形来近似这一点(但这似乎是一种黑客行为,而且我不确定是否可能)。
I would like to be able to produce a stacked line graph (similar to the method used here) with Python (preferably using matplotlib, but another library would be fine too). How can I do this?
This similar to the stacked bar graph example on their website, except I'd like the top of bar to be connected with a line segment and the area underneath to be filled. I might be able to approximate this by decreasing the gaps between bars and using lots of bars (but this seems like a hack, and besides I'm not sure if it is possible).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
较新版本的 matplotlib 包含函数
plt.stackplot()< /code>
,它允许几种不同的“开箱即用”堆叠面积图:
Newer versions of matplotlib contain the function
plt.stackplot()
, which allows for several different "out-of-the-box" stacked area plots:我相信面积图是此类图的常用术语,并且在OP中引用的特定实例中,堆积面积图。
Matplotlib 没有结合数据处理和绘图/渲染步骤来创建此类绘图的“开箱即用”功能,但您可以轻松地从组件中创建自己的绘图由 Matplotlib 和 NumPy 提供。
下面的代码首先堆叠数据,然后绘制绘图。
I believe Area Plot is a common term for this type of plot, and in the specific instance recited in the OP, Stacked Area Plot.
Matplotlib does not have an "out-of-the-box" function that combines both the data processing and drawing/rendering steps to create a this type of plot, but it's easy to roll your own from components supplied by Matplotlib and NumPy.
The code below first stacks the data, then draws the plot.
如果您有数据框,则非常简单:
来自:熊猫文档
If you have a dataframe, it's quite easy:
From: pandas documentation
一种稍微不那么黑客化的方法是首先使用折线图和 matplotlib.pyplot.fill_ Between 。要模拟堆叠,您必须自己将点向上移动。
A slightly less hackish way would be to use a line graph in the first place and
matplotlib.pyplot.fill_between
. To emulate the stacking you have to shift the points up yourself.