如何在python中直观地堆叠多个折线图? [英] How do I visually stack multiple line graphs above each other in python?
问题描述
我在创建需要多个线图的绘图时遇到了麻烦.
I was having trouble creating a plot I need which has multiple line graphs.
我想要的是一种在彼此之上绘制每个图的方法(例如,一个基线的y = 5,我希望下一个基线的y = 10),而且这些图中的每一个都必须阻止一个它上面.
What I want is a way to graph each of these above the other (say one has a baseline of y=5 I want the next to have a baseline of y=10) and also each of these graphs must block the one above it.
因此,这不可避免地看起来像Joy Joys Divisions Unknown Pleasures的封面: http://cococubed.asu.edu/images/unknown_pleasures/unknown_pleasures.jpg
So this will inevitably look like the cover to Joy Divisions Unknown Pleasures here: http://cococubed.asu.edu/images/unknown_pleasures/unknown_pleasures.jpg
除了倒置的颜色,我也想要一个使用python或numpy或matplotlib的答案.
Except inverted colors and I also would like an answer that utilizes python or numpy or matplotlib.
推荐答案
这是一种方法.关键是要使用fill_between函数,并以一定的边距(在本例中为i*2
)偏移每条绘制的线.另外,绘图必须从顶部开始,因此是[range:]切片中的[::-1].
Here's one way. The key point is to use fill_between function and offset each plotted line with some margin (i*2
in this case). Also, plotting has to start from the top, hence the [::-1] in the arange slice.
t=linspace(-2*pi, 2*pi, 1000)
for i in arange(1, pi, 0.01)[::-1]:
left = exp(-(t + (i - 1) * 2*pi)**2) * cos(t * i)**2 - 1
right = exp(-(t - (i - 1) * 2*pi)**2) * cos(t * i)**2 - 1
vertical_offset = i*2
fill_between(t, vertical_offset + left + right, facecolor='white')
这篇关于如何在python中直观地堆叠多个折线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!