英文:
How to change the color of line plotted on y axis midway in plotly?
问题
我有一个包含两列的数据框:'Ground Truth' 和 'Predicted Values',我正在使用plotly express绘制它。
timestamp Ground Truth Predicted Values
2012-04-01 00:30:00 251.71 NA
2012-04-01 00:15:00 652.782 NA
2012-04-01 00:00:00 458.099 NA
2012-03-31 23:45:00 3504.664 NA
2012-03-31 23:30:00 1215.76 1230
2012-03-31 23:15:00 -21.48 -19.99
2012-03-31 23:00:00 -8.538 -7.42
2012-03-31 22:40:00 -5.11 -5.2
绘图代码
fig = px.line(df, x = df.index, y = ['Ground Truth','Predicted Values'], markers='.')
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count = 1, label = "1H", step = "hour", stepmode ="backward"),
dict(step="all")
])
)
)
fig.show()
使用当前的代码,每列都绘制成一条线(如下所示)。我想要更改图形,使蓝线从红线的开始处改为其他颜色。有人能帮忙吗?(参考数据框,红线代表'Predicted values'列,从NA值结束后开始)。
当前图表(在不同值上绘制):
[![点击此处输入图像描述][1]][1]
[1]: https://i.stack.imgur.com/HHdga.png
英文:
I have a dataframe with 2 columns: 'Ground Truth' and 'Predicted Values', that I am plotting using plotly express.
timestamp Ground Truth Predicted Values
2012-04-01 00:30:00 251.71 NA
2012-04-01 00:15:00 652.782 NA
2012-04-01 00:00:00 458.099 NA
2012-03-31 23:45:00 3504.664 NA
2012-03-31 23:30:00 1215.76 1230
2012-03-31 23:15:00 -21.48 -19.99
2012-03-31 23:00:00 -8.538 -7.42
2012-03-31 22:40:00 -5.11 -5.2
Code for plot
fig = px.line(df, x = df.index, y = ['Ground Truth','Predicted Values'], markers='.')
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count = 1, label = "1H", step = "hour", stepmode ="backward"),
dict(step="all")
])
)
)
fig.show()
With the current code, one line for each column is being plotted (As per given below). I am trying to change the graph, in which the blue line's changes to some other color from the start of the red line. Can someone please help? (In reference to the dataframe, the red line is for 'Predicted values' columns which starts after the NA values end).
答案1
得分: 1
我根据@r-beginners上面的评论得到的帮助解决了它。我将代码添加在这里,以防对任何人有所帮助。
使用布尔索引。我在DataFrame中创建了一个名为'New Column'的新列,并将其填充为NaN值。然后使用布尔索引将'Predicted Values'不是NaN的值设置到'New Column'中对应的'Ground Truth'值上。~运算符反转了isna()方法生成的布尔掩码,因此我选择了'Predicted Values'不是NaN的行。
# 创建一个新列,并在'Predicted Values'不是NaN的情况下用'Ground Truth'值填充它
df['New Column'] = np.nan
df.loc[~df['Predicted Values'].isna(), 'New Column'] = df.loc[~df['Predicted Values'].isna(), 'Ground Truth']
英文:
I managed to solve it based on the help from @r-beginners comment above. I'll add the code here, in case it might help anyone.
Use boolean indexing. I create a new column in the DataFrame called 'New Column' and fill it with NaN values. Then used boolean indexing to set the values in 'New Column' where 'Predicted Values' are not NaN to the corresponding values in 'Ground Truth'. The ~ operator inverts the boolean mask generated by the isna() method, so I am selecting rows where 'Predicted Values' are not NaN.
# Create a new column and populate it with 'Ground Truth' values where 'Predicted Values' are not NaN
df['New Column'] = np.nan
df.loc[~df['Predicted Values'].isna(), 'New Column'] = df.loc[~df['Predicted Values'].isna(), 'Ground Truth']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论