繁体   English   中英

Plotly:如何为散点图中的每个系列设置唯一的颜色?

[英]Plotly: How to set a unique color for each series in a scatter plot?

我在 Pandas 中有一个数据框,以股票代码作为索引和 2 列“活动权重”和“权重”。 我可以使用下面的代码制作散点图,但我想为每个股票代码使用唯一的颜色。 我该怎么做?

    scatter = [go.Scatter(x = df['Active Weight'], y = df['Weight'],
    mode='markers', text=df.index)]

    plotly.offline.iplot(scatter)

我可能在这里遗漏了一些东西,但从它的声音来看,您实际上只是在寻找这样的散点图:

在此处输入图片说明

下面的代码设置为遵循 plotly 的默认颜色循环。 但是您可以将其从 plotly express 更改为任何其他颜色方案,例如px.colors.qualitative.Plotly 或者只是让你自己喜欢 ['black', 'yellow']。

完整代码:

# imports
import plotly.express as px
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data 1
np.random.seed(123)
frame_rows = 40
frame_columns = ['Active Weight', 'Weight']
df= pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# color settings
#colors=px.colors.qualitative.plotly 
#colors=px.colors.qualitative.Dark24_r 
colors = ['black', 'yellow']

# plotly figure
fig = go.Figure()
for i, col in enumerate(df):
    fig.add_traces(go.Scatter(x=df.index, y = df[col].values,
                              mode = 'markers',
                              name = col,
                              marker=dict(color=colors[i])))

fig.show()

暂无
暂无

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

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