【问题标题】:Plotly: How to set a unique color for each series in a scatter plot?Plotly:如何为散点图中的每个系列设置唯一的颜色?
【发布时间】:2017-09-28 07:06:32
【问题描述】:

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

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

    plotly.offline.iplot(scatter)

【问题讨论】:

  • 如果我错了,请纠正我,但你想让每个散点有不同的颜色吗?颜色可以随意吗?
  • 每个散点没有不同的颜色。当然颜色可以是随机的。我有每个点的标签。因此,对于所有带有 xxx 代码的点,我想用相同的颜色显示它们。代码 活动体重 体重 xxx x1 y1 xxx x2 y2 yyy x3 y3
  • @user3381431 我的建议对你有什么效果?

标签: python plotly


【解决方案1】:

我可能在这里遗漏了一些东西,但听上去,您实际上只是在寻找这样的散点图:

下面的代码设置为遵循 plotly 的默认颜色循环。但是您可以将其更改为任何其他配色方案,例如 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()

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2020-12-20
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多