[英]if slope is greater than some threshold, then color the line red in python
I have a line graph that I'm plotting in python. The last thing that I need to do is add a line telling it to color a section of the graph red if the slope is greater that 25. How would I do this?我在 python 中绘制了一个折线图。我需要做的最后一件事是添加一条线,告诉它在斜率大于 25 时将图形的一部分着色为红色。我该怎么做?
Here's what I have so far.这是我到目前为止所拥有的。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('ReactorStartupTemps.csv')
print(data)
plt.rcParams["figure.figsize"] = [5, 4]
plt.rcParams["figure.autolayout"] = True
plt.xlabel('Time [min]', fontdict={'fontname': 'Times New Roman',
'style': 'italic'})
plt.ylabel('Temperature [C]', fontdict={'fontname': 'Times New Roman',
'style': 'italic'})
plt.title('Reactor Startup Temperatures', fontdict={'fontname': 'Times
New Roman', 'style': 'italic'})
[slope, intercept] = np.polyfit(data.Time, data.Temp, 1)
if slope > 25:
plt.plot(data.Time, data.Temp, color="red", linewidth=1.5)
else:
plt.plot(data.Time, data.Temp, color="black", linewidth=1.5)
plt.minorticks_on()
plt.grid(which='major', color='grey', linewidth=.8)
plt.grid(which='minor', color='lightgrey', linewidth=.4, ls='--')
plt.show()
What the graph needs to look like:图形需要看起来像什么:
Graph as of now:截至目前的图表:
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('ReactorStartupTemps.csv')
print(data)
plt.rcParams["figure.figsize"] = [7, 5]
plt.rcParams["figure.autolayout"] = True
plt.xlabel('Time [min]', fontdict = {'fontname': 'Times New Roman',
'style': 'italic'})
plt.ylabel('Temperature [C]', fontdict = {'fontname': 'Times New
Roman',
'style': 'italic'})
plt.title('Reactor Startup Temperatures', fontdict = {'fontname':
'Times
New Roman', 'style': 'italic'})
slope, intercept = np.polyfit(np.log(data.Time), np.log(data.Temp), 1)
if slope > 25 :
plt.plot(data.Time, data.Temp, color = "red",linewidth = 1.5)
else:
plt.plot(data.Time, data.Temp, color = "black",linewidth = 1.5)
plt.minorticks_on()
plt.grid(which = 'major', color = 'grey', linewidth = .8)
plt.grid(which = 'minor', color = 'lightgrey', linewidth = .4, ls =
'--')
plt.show()
first you have to calculate slope and then you can implement your condition首先你必须计算斜率然后你可以实现你的条件
for slope calculation用于坡度计算
slope, intercept = np.polyfit(np.log(data.Time), np.log(data.Temp), 1)
Currently you are using the slope of the regression line.目前您正在使用回归线的斜率。 np.polyfit
performs least squares and returns the slope of the fitted line, which is not what you want. np.polyfit
执行最小二乘法并返回拟合线的斜率,这不是您想要的。
Instead you're looking for the slope between all consecutive points:相反,您正在寻找所有连续点之间的斜率:
Series.diff
to compute (y2-y1) / (x2-x1)
for all consecutive points使用Series.diff
计算所有连续点(y2-y1) / (x2-x1)
Series.where
to mask the segments where the slope exceeds 25使用Series.where
屏蔽斜率超过 25 的线段zorder
to raise the masked segments使用高zorder
来提高屏蔽段THRESHOLD = 25
slopes = data.Temp.diff() / data.Time.diff()
plt.plot(data.Time, data.Temp, color='k')
plt.plot(data.Time, data.Temp.where(slopes > THRESHOLD), color='r', zorder=10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.