python stackplot_Python Matplotlib Stackplot中的标签区域

I would like to generate labels inside the areas of a matplotlib stackplot. I would settle for labeling a line used to bound the area. Consider the example:

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

This produces:

But I would like to produce something like this:

The matplotlib contour plots have this type of labeling functionality (though the lines are labeled in the case of the contour plot).

Any help (or even redirection to a post I might have missed) is appreciated.

解决方案

Ah, heuristics. Something like this?:

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

which in test runs is okayish:

Finding the best loc could be fancier -- maybe one wants the x_n, x_(n+1) with the highest average value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值