如何创建堆积线图?

发布于 2024-08-20 12:54:24 字数 441 浏览 7 评论 0原文

我希望能够生成堆积线图(类似于使用的方法 这里)使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑看君怀她人 2024-08-27 12:54:24

较新版本的 matplotlib 包含函数 plt.stackplot()< /code>,它允许几种不同的“开箱即用”堆叠面积图:

import numpy as np
import pylab as plt

X = np.arange(0, 10, 1) 
Y = X + 5 * np.random.random((5, X.size))

baseline = ["zero", "sym", "wiggle", "weighted_wiggle"]
for n, v in enumerate(baseline):
    plt.subplot(2 ,2, n + 1)
    plt.stackplot(X, *Y, baseline=v)
    plt.title(v)
    plt.axis('tight')
plt.show()

堆叠图使用 plt.stackplot。

Newer versions of matplotlib contain the function plt.stackplot(), which allows for several different "out-of-the-box" stacked area plots:

import numpy as np
import pylab as plt

X = np.arange(0, 10, 1) 
Y = X + 5 * np.random.random((5, X.size))

baseline = ["zero", "sym", "wiggle", "weighted_wiggle"]
for n, v in enumerate(baseline):
    plt.subplot(2 ,2, n + 1)
    plt.stackplot(X, *Y, baseline=v)
    plt.title(v)
    plt.axis('tight')
plt.show()

Stack plot using plt.stackplot.

如何视而不见 2024-08-27 12:54:24

我相信面积图是此类图的常用术语,并且在OP中引用的特定实例中,堆积面积图

Matplotlib 没有结合数据处理和绘图/渲染步骤来创建此类绘图的“开箱即用”功能,但您可以轻松地从组件中创建自己的绘图由 Matplotlib 和 NumPy 提供。

下面的代码首先堆叠数据,然后绘制绘图。

import numpy as NP
from matplotlib import pyplot as PLT

# just create some random data
fnx = lambda : NP.random.randint(3, 10, 10)
y = NP.row_stack((fnx(), fnx(), fnx()))   
# this call to 'cumsum' (cumulative sum), passing in your y data, 
# is necessary to avoid having to manually order the datasets
x = NP.arange(10) 
y_stack = NP.cumsum(y, axis=0)   # a 3x10 array

fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.fill_between(x, 0, y_stack[0,:], facecolor="#CC6666", alpha=.7)
ax1.fill_between(x, y_stack[0,:], y_stack[1,:], facecolor="#1DACD6", alpha=.7)
ax1.fill_between(x, y_stack[1,:], y_stack[2,:], facecolor="#6E5160")

PLT.show()

堆积面积图示例

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.

import numpy as NP
from matplotlib import pyplot as PLT

# just create some random data
fnx = lambda : NP.random.randint(3, 10, 10)
y = NP.row_stack((fnx(), fnx(), fnx()))   
# this call to 'cumsum' (cumulative sum), passing in your y data, 
# is necessary to avoid having to manually order the datasets
x = NP.arange(10) 
y_stack = NP.cumsum(y, axis=0)   # a 3x10 array

fig = PLT.figure()
ax1 = fig.add_subplot(111)

ax1.fill_between(x, 0, y_stack[0,:], facecolor="#CC6666", alpha=.7)
ax1.fill_between(x, y_stack[0,:], y_stack[1,:], facecolor="#1DACD6", alpha=.7)
ax1.fill_between(x, y_stack[1,:], y_stack[2,:], facecolor="#6E5160")

PLT.show()

example of stacked area plot

梦萦几度 2024-08-27 12:54:24

如果您有数据框,则非常简单:

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area();

在此处输入图像描述

来自:熊猫文档

If you have a dataframe, it's quite easy:

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area();

enter image description here

From: pandas documentation

厌味 2024-08-27 12:54:24

一种稍微不那么黑客化的方法是首先使用折线图和 matplotlib.pyplot.fill_ Between 。要模拟堆叠,您必须自己将点向上移动。

x = np.arange(0,4)
y1 = np.array([1,2,4,3])
y2 = np.array([5,2,1,3])
# y2 should go on top, so shift them up
y2s = y1+y2

plot(x,y1)
plot(x,y2s)
fill_between(x,y1,0,color='blue')
fill_between(x,y1,y2s,color='red')

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.

x = np.arange(0,4)
y1 = np.array([1,2,4,3])
y2 = np.array([5,2,1,3])
# y2 should go on top, so shift them up
y2s = y1+y2

plot(x,y1)
plot(x,y2s)
fill_between(x,y1,0,color='blue')
fill_between(x,y1,y2s,color='red')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文