简体   繁体   English

如何使用where参数使用fill_between

[英]How to use fill_between utilizing the where parameter

So following a tutorial, I tried to create a graph using the following code: 因此,按照一个教程,我尝试使用以下代码创建图形:

time_values = [i for i in range(1,100)]
execution_time = [random.randint(0,100) for i in range(1,100)]
fig = plt.figure()
ax1 = plt.subplot()
threshold=[.8 for i in range(len(execution_time))]
ax1.plot(time_values, execution_time)
ax1.margins(x=-.49, y=0)
ax1.fill_between(time_values,execution_time, 1,where=(execution_time>1), color='r', alpha=.3)

This did not work as I got an error saying I could not compare a list and an int. 这没有用,因为我收到一个错误消息,说我无法比较列表和整数。 However, I then tried: 但是,我然后尝试:

ax1.fill_between(time_values,execution_time, 1)

And that gave me a graph with all area in between the execution time and the y=1 line, filled in. Since I want the area above the y=1 line filled in, with the area below left un-shaded, I created a list called threshold, and populated it with 1 so that I could recreate the comparison. 这给了我一张图形,其中包含了执行时间和y = 1行之间的所有区域,已填充。由于我希望填充y = 1行以上的区域,而下面的区域未阴影,因此创建了一个列表称为阈值,并用1填充它,以便我可以重新创建比较。 However, 然而,

ax1.fill_between(time_values,execution_time, 1,where=(execution_time>threshold)

and

ax1.fill_between(time_values,execution_time, 1)

create the exact same graph, even though the execution times values do go beyond 1. 即使执行时间值确实超过1,也可以创建完全相同的图。

I am confused for two reasons: firstly, in the tutorial I was watching, the teacher was able to successfully compare a list and an integer within the fill_between function, why was I not able to do this? 我感到困惑的原因有两个:首先,在我正在观看的教程中,老师能够成功比较fill_between函数中的列表和整数,为什么我不能这样做? Secondly, why is the where parameter not identifying the regions I want to fill? 其次,为什么where参数不能标识我要填充的区域? Ie, why is the graph shading in the areas between the y=1 and the value of the execution time? 即,为什么在y = 1和执行时间的值之间的区域中出现图形阴影?

The problem is mainly due the use of python lists instead of numpy arrays. 问题主要是由于使用python列表而不是numpy数组。 Clearly you could use lists, but then you need to use them throughout the code. 显然,您可以使用列表,但是随后您需要在整个代码中使用它们。

import numpy as np
import matplotlib.pyplot as plt

time_values = list(range(1,100))
execution_time = [np.random.randint(0,100) for _ in range(len(time_values))]
threshold = 50


fig, ax = plt.subplots()

ax.plot(time_values, execution_time)
ax.fill_between(time_values, execution_time, threshold,
                where= [e > threshold for e in execution_time], 
                color='r', alpha=.3)

ax.set_ylim(0,None)
plt.show()

Better is the use of numpy arrays throughout. 更好的是始终使用numpy数组。 It's not only faster, but also easier to code and understand. 它不仅速度更快,而且更易于编码和理解。

import numpy as np
import matplotlib.pyplot as plt

time_values = np.arange(1,100)
execution_time = np.random.randint(0,100, size=len(time_values))
threshold = 50


fig, ax = plt.subplots()

ax.plot(time_values, execution_time)
ax.fill_between(time_values,execution_time, threshold,
                where=(execution_time > threshold), color='r', alpha=.3)

ax.set_ylim(0,None)
plt.show()

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

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