Ploty,单色多彩的线

huangapple go评论66阅读模式
英文:

Ploty, single multicolored line

问题

I have a data frame with a date and two columns.

这是一个包含日期和两列的数据框。

This is not the real data. The plot is actually working better here than with my real data. In my real data, the 3 lines are continuous, over here at least two additional lines are broken.

这不是真实数据。在这里,图表的效果实际上比我的真实数据好。在我的真实数据中,三条线是连续的,但在这里至少有两条额外的线断开。

How to get one line that changes color instead of three separate lines?

如何获取一条线,并使其根据颜色变化,而不是三条单独的线?

The data looks like this:

数据看起来像这样:

date P1 c1
2021/01/07 2 0
2021/01/08 4 1
2021/01/11 6 1
2021/01/12 6 0
2021/01/13 4 -1
2021/01/14 2 -1
2021/01/15 2 0

c1 shows when p1 is rising or falling. I want to plot p1 colored by c1. I don't want multiple lines.

c1 表示 p1 是上升还是下降。我想绘制根据 c1 着色的 p1。我不想要多条线。

import pandas as pd
import plotly.express as px

import pandas as pd

data = {
  "date": ['2021/01/07', '2021/01/08', '2021/01/11', '2021/01/12', '2021/01/13', '2021/01/14', '2021/01/15'],
  "p1": [2, 4, 6, 6, 4, 2, 2],
  "c1": [0, 1, 1, 0, -1, -1, 0]
}
df = pd.DataFrame(data)

df['date1'] = pd.to_datetime(df['date'])

fig = px.line(df, x='date1', y='p1', color='c1')
fig.show()

以上是您要的翻译部分。

英文:

I have a data frame with a date and two columns.

This is not the real data. The plot is actually working better here than with my real data. In my real data, the 3 lines are continuous, over here at least two additional lines are broken.

How to get one line that changes color instead of three separate lines?

The data looks like this:

date P1 c1
2021/01/07 2 0
2021/01/08 4 1
2021/01/11 6 1
2021/01/12 6 0
2021/01/13 4 -1
2021/01/14 2 -1
2021/01/15 2 0

c1 shows when p1 is rising or falling. I want to plot p1 colored by c1. I don't want multiple lines.

import pandas as pd
import plotly.express as px

import pandas as pd

data = {
  "date": ['2021/01/07', '2021/01/08', '2021/01/11', '2021/01/12','2021/01/13','2021/01/14','2021/01/15'],
  "p1": [2, 4, 6, 6,4, 2,2],
  "c1":[0,1,1,0,-1,-1,0]}
df = pd.DataFrame(data)

df['date1'] = pd.to_datetime(df['date'])


fig=px.line(df, x='date1',y='p1',color='c1')
#fig=px.line(df, x='Date',y='t1')
#fig=px.line(df, x='date1',y='p1',facet_col='c1')
fig.show()

答案1

得分: 1

Plotly 不原生支持此功能,但您可以为每个唯一的 "c1" 值绘制单独的线,然后将它们连接起来,例如:

import pandas as pd
import plotly.graph_objects as go

data = {
    "date": ['2021/01/07', '2021/01/08', '2021/01/11', '2021/01/12','2021/01/13','2021/01/14','2021/01/15'],
    "p1": [2, 4, 6, 6, 4, 2, 2],
    "c1":[0, 1, 1, 0, -1, -1, 0]
}
df = pd.DataFrame(data)

df['date'] = pd.to_datetime(df['date'])

colors = {0: 'blue', 1: 'green', -1: 'red'} 

fig = go.Figure()

for c1_val in df['c1'].unique():
    df_sub = df[df['c1'] == c1_val]
    fig.add_trace(go.Scatter(x=df_sub['date'], y=df_sub['p1'], mode='lines', line=dict(color=colors[c1_val])))

fig.show()

希望这有所帮助。

英文:

Plotly doesn't support this functionality natively but you can plot separate lines for each unique 'c1' value and then join them together, for example:

import pandas as pd
import plotly.graph_objects as go

data = {
    "date": ['2021/01/07', '2021/01/08', '2021/01/11', '2021/01/12','2021/01/13','2021/01/14','2021/01/15'],
    "p1": [2, 4, 6, 6, 4, 2, 2],
    "c1":[0, 1, 1, 0, -1, -1, 0]
}
df = pd.DataFrame(data)

df['date'] = pd.to_datetime(df['date'])

colors = {0: 'blue', 1: 'green', -1: 'red'} 

fig = go.Figure()

for c1_val in df['c1'].unique():
    df_sub = df[df['c1'] == c1_val]
    fig.add_trace(go.Scatter(x=df_sub['date'], y=df_sub['p1'], mode='lines', line=dict(color=colors[c1_val])))

fig.show()

I hope this helps.

huangapple
  • 本文由 发表于 2023年6月1日 07:16:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定