如何在matplotlib中局部进行fill_between处理,如不同的颜色表示不同的值 [英] How to partial fill_between in matplotlib, as in different colors for different values

查看:54
本文介绍了如何在matplotlib中局部进行fill_between处理,如不同的颜色表示不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为图形线和x轴之间的空间着色.颜色应基于线上相应点的值.有点像第一张图:(

值小于100的区域应为绿色.但是,线和x轴之间的间隔始终是基于数组中第一个值(在此示例中为红色)的颜色.我该如何解决?

解决方案

使用 numpy A>B .否则,如果不想使用numpy,则需要使用 [a>b代表zip(A,B)中的a,b] .

 将numpy导入为np导入matplotlib.pyplot作为plty = [107,108,105,109,107,106,107,109,106,106,94,93,94,93,93,94,95,106,108,109、107、107、106、108、105、108、107、106、107、97、93、96、94、96、95、94、104、107,106、108、107、107、106、107、105、107、108、105、107、100、93、94、93、95、104、107、107,108、108、107、107、107、107、104、94、96、95、96、94、95、94、100、107、107、105、107、107,109、107、108、107、105、108、108、106、97、94、94、94、94、95、94、94、94、96、108、108、107,106、107、107、108、107、106、95、95、95、94、94、96、105、108、107、106、106、108、107,108、106、107]y = np.array(y)x = np.arange(len(y))lvl = 100无花果,ax = plt.subplots()ax.plot(x,y,color ="black")ax.fill_between(x,0,y,where = y> lvl,facecolor ='red',interpolate = True)ax.fill_between(x,0,y,其中= y <= lvl,facecolor ='绿色',插值= True)plt.show() 

I'm trying to color the space between the graph line and the x-axis. The color should be based on the value of the corresponding point on the line. Kinda like the first graph: (https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/fill_between_demo.html)

If it's higher than 100 it should be red, if it's lower than 100 it should be green. The graph I'm looking for should be red-green alternating.

This is what I have at the moment:

import matplotlib.pyplot as plt

lenght = 120

y = [107, 108, 105, 109, 107, 106, 107, 109, 106, 106, 94, 93, 94, 93, 93, 94, 95, 106, 108, 109, 107, 107, 106, 108, 105, 108, 107, 106, 107, 97, 93, 96, 94, 96, 95, 94, 104, 107, 106, 108, 107, 107, 106, 107, 105, 107, 108, 105, 107, 100, 93, 94, 93, 95, 104, 107, 107, 108, 108, 107, 107, 107, 107, 104, 94, 96, 95, 96, 94, 95, 94, 100, 107, 107, 105, 107, 107, 109, 107, 108, 107, 105, 108, 108, 106, 97, 94, 94, 94, 94, 95, 94, 94, 94, 96, 108, 108, 107, 106, 107, 107, 108, 107, 106, 95, 95, 95, 94, 94, 96, 105, 108, 107, 106, 106, 108, 107, 108, 106, 107]

x = [x for x in range(lenght)]

lvl = lenght * [100]

fig, ax = plt.subplots()

ax.plot(x, y, color="black")
ax.fill_between(x, 0, y, where=y>lvl, facecolor='red', interpolate=True)
ax.fill_between(x, 0, y, where=y<=lvl, facecolor='green', interpolate=True)

plt.show()

This results in the following graph:

The area's where the value is less than 100 should be green. But the space between the line and the x-axis is always the color based on the first value in the array (in this example, red). How can I fix this?

解决方案

Use numpy, A > B. Else, if not wanting to use numpy it would need to be [a > b for a,b in zip(A,B)].

import numpy as np
import matplotlib.pyplot as plt

y = [107, 108, 105, 109, 107, 106, 107, 109, 106, 106, 94, 93, 94, 93, 93, 94, 95, 106, 108, 
     109, 107, 107, 106, 108, 105, 108, 107, 106, 107, 97, 93, 96, 94, 96, 95, 94, 104, 107, 
     106, 108, 107, 107, 106, 107, 105, 107, 108, 105, 107, 100, 93, 94, 93, 95, 104, 107, 107, 
     108, 108, 107, 107, 107, 107, 104, 94, 96, 95, 96, 94, 95, 94, 100, 107, 107, 105, 107, 107, 
     109, 107, 108, 107, 105, 108, 108, 106, 97, 94, 94, 94, 94, 95, 94, 94, 94, 96, 108, 108, 107, 
     106, 107, 107, 108, 107, 106, 95, 95, 95, 94, 94, 96, 105, 108, 107, 106, 106, 108, 107, 
     108, 106, 107]
y = np.array(y)
x = np.arange(len(y))

lvl = 100

fig, ax = plt.subplots()

ax.plot(x, y, color="black")
ax.fill_between(x, 0, y, where=y>lvl, facecolor='red', interpolate=True)
ax.fill_between(x, 0, y, where=y<=lvl, facecolor='green', interpolate=True)

plt.show()

这篇关于如何在matplotlib中局部进行fill_between处理,如不同的颜色表示不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆