python - Python Matplotlib stackplot 中的标签区域

标签 python matplotlib plot stacked-chart stacked-area-chart

我想在 matplotlib 堆栈图的区域内生成标签。我会满足于标记一条用于限制该区域的线。考虑这个例子:

import numpy as np
from matplotlib import pyplot as plt

fnx = lambda : np.random.randint(5, 50, 10)
x = np.arange(10)
y1, y2, y3 = fnx(), fnx(), fnx()
areaLabels=['area1','area2','area3']
fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3)
plt.show()

这会产生:enter image description here

但我想制作这样的东西:

enter image description here

matplotlib 等高线图具有这种类型的标记功能(尽管在等高线图的情况下线条被标记)。

感谢任何帮助(甚至重定向到我可能错过的帖子)。

最佳答案

啊,启发式。是这样的吗?:

import numpy as np
from matplotlib import pyplot as plt

length = 10
fnx = lambda : np.random.randint(5, 50, length)
x = np.arange(length)
y1, y2, y3 = fnx(), fnx(), fnx()
areaLabels=['area1','area2','area3']
fig, ax = plt.subplots()
ax.stackplot(x, y1, y2, y3)

loc = y1.argmax()
ax.text(loc, y1[loc]*0.25, areaLabels[0])

loc = y2.argmax()
ax.text(loc, y1[loc] + y2[loc]*0.33, areaLabels[1])

loc = y3.argmax()
ax.text(loc, y1[loc] + y2[loc] + y3[loc]*0.75, areaLabels[2]) 

plt.show()

在测试运行中还可以:

enter image description here

enter image description here

enter image description here

找到最好的 loc 可能更有趣——也许有人想要具有最高平均值的 x_n, x_(n+1)

关于python - Python Matplotlib stackplot 中的标签区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31573869/

相关文章:

R 在设置宽度和高度的 PDF 中缩放绘图元素

python - 改变 Pandas 图的颜色条

python - 如何使python自动完成显示匹配?

python - 当数据在元组中时如何在 matplotlib 中绘制直方图?

Python绘图强制按字母顺序而不是按时间顺序对日期进行排序

matplotlib - 在 mplot3d 中绘制右手坐标系

python - 在Python的同一列中绘制数据

python - 是否有与 Laravel 4 等效的 python?

python - 检测字符串列表中的年份

python - 为什么这个简单的 python 程序不能按我想要的方式工作?