简体   繁体   English

如何使用pylab(pyplot)中的步进线(步进曲线)填充两个不同的colors的区域?

[英]How to fill areas with two different colors using step lines (step curves) in pylab (pyplot)?

This is my first question, so be kind!这是我的第一个问题,所以请客气!

I've plotted two lines (step lines), y1 and y2, where y1 is a list with some random numbers and y2 is each previous number from y1 list.我绘制了两条线(步进线),y1 和 y2,其中 y1 是一个包含一些随机数的列表,y2 是 y1 列表中的每个前一个数字。

from pylab import *

y1 = ([100, 101, 102, 103, 102, 101, 102, 103])
y2 = ([100, 100, 101, 102, 103, 102, 101, 102])
x = (list(range(len(y1))))

plot(x, y1,  linestyle='steps', drawstyle="steps")
plot(x, y2,  linestyle='steps', drawstyle="steps")
xlabel('No.')
ylabel('Level')
title('Step Lines Chart')
grid(True)
fill_between(x, y1, y2, where=(y1 > y2), color='C0', alpha=0.3, step="pre")
fill_between(x, y1, y2, where=(y1 < y2), color='C1', alpha=0.3, step="pre")
show()

在此处输入图像描述

From here I want to fill between these lines with two colors, when y1 > y2 with a color and when y1 < y2 with another color, but I am able to fill only with one color...从这里开始,我想用两条 colors 在这些行之间填充,当 y1 > y2 有一种颜色时,当 y1 < y2 有另一种颜色时,但我只能用一种颜色填充......

I tried with where=(y1 < y2)... condition, also tried to replicate the docs example from here , but for no reason will not pop up the window with the chart.我尝试使用where=(y1 < y2)...条件,还尝试从此处复制文档示例,但无缘无故不会弹出带有图表的 window。

So I'm kinda stuck...所以我有点卡住了...

Ok, I used numpy array and it worked partially, if y1 and y2 are like in the example, both colors will be plotted only when the square rise or fall, but when two squares are at the same level, there is no fill...好的,我使用了 numpy 数组并且它部分工作,如果 y1 和 y2 像示例中那样,则 colors 将仅在方块上升或下降时绘制,但当两个方块处于同一水平时,没有填充.. . 在此处输入图像描述

from pylab import *
import numpy as np

y1 = np.array([100, 101, 102, 103, 102, 101, 102, 103])
y2 = np.array([100, 100, 101, 102, 103, 102, 101, 102])

#y1 = np.array([100,100, 101,101, 102,102, 103,103, 102,102, 101,101, 102,102, 103,103])
#y2 = np.array([100,100, 100,100, 101,101, 102,102, 103,103, 102,102, 101,101, 102,102])

x = np.array(list(range(len(y1))))

plot(x, y1, '-', drawstyle="steps")
plot(x, y2, '-', drawstyle="steps")
xlabel('No.')
ylabel('Level')
title('Step Lines Chart')
grid(True)
fill_between(x, y1, y2, where=(y1 > y2), color='C0', alpha=0.3, step="pre")
fill_between(x, y1, y2, where=(y1 < y2), color='C1', alpha=0.3, step="pre")
fill_between(x, y1, y2, where=(y1 == y2), color='yellow', alpha=0.3, step="pre")
show()

I tried to double all numbers (see commented syntax) and worked a little bit better, but not still is not perfect, now will plot well, but when two squares are at the same level, will fill only half of the square我尝试将所有数字加倍(参见注释语法)并且工作得更好一点,但仍然不完美,现在 plot 很好,但是当两个正方形处于同一水平时,只会填充正方形的一半

在此处输入图像描述

All I need is when y1(blue line) is greater than y2(orange line), the square to be filled with blue and y1 < y2 filled with orange color我只需要当 y1(蓝线)大于 y2(橙线)时,用蓝色和 y1 < y2 填充橙色的正方形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM