简体   繁体   English

如何控制 plotly 条形图中的彩条重复?

[英]How to control color bar repeating in plotly bar chart?

I have created a plotted bar chart with Plotly where I noticed that colours are repeated when bars are above 10. I want to have control over this when the length of the x-axis is more than 10, pass a list of colours to be used.我用 Plotly 创建了一个绘制的条形图,其中我注意到当条形大于 10 时颜色会重复。当 x 轴的长度超过 10 时,我想对此进行控制,传递要使用的颜色列表. There are two orange, two purple, two sky blue, two blue, etc..有两个橙色,两个紫色,两个天蓝色,两个蓝色等。 在此处输入图像描述

This is the code这是代码

df = common_helpers.get_dataframe_in_period(df, options)
if "All" not in options["clinic_nurse"]:
   df = df[df["nurse_name"].isin(options["clinic_nurse"])]
per = df.index_date.dt.to_period(options["groupby"]).astype(str)
count = df.pivot_table(
        index=per, columns="nurse_name", values="patient_count", aggfunc="sum"
    ).fillna(0)
    data = []
for col in count.columns:
        data.append(go.Bar(name=col, x=count.index, y=count[col]))
fig = go.Figure(data)

By default, plotly has up to 10 colors.默认情况下,plotly 最多有 10 个 colors。 But you can control and set your favorite color sequency, for example, in px bar parameters by adding:但是您可以控制和设置您喜欢的颜色顺序,例如,在 px bar 参数中通过添加:

color_discrete_sequence=px.colors.qualitative.Dark24 color_discrete_sequence=px.colors.qualitative.Dark24

I guess that, if you use go.bar it's possible to use the same trick.我想,如果您使用 go.bar ,则可以使用相同的技巧。 Check out this source to find the color sequences with plotly.查看此来源以查找 plotly 的颜色序列。

https://plotly.com/python/discrete-color/ https://plotly.com/python/discrete-color/

I've found the solution and did it finally.我找到了解决方案并最终做到了。 Just import plotly.io which will help you modify the main Plotly colours list and append it to whatever number of colours you want to have只需导入 plotly.io 这将帮助您修改主要的 Plotly 颜色列表和 Z9516DFB15F51C7EE19ZD4 的任何数量的颜色

import plotly.io as pio
plotly_template = pio.templates["plotly"]
pio.templates["draft"] = plotly_template
additional_colors = ("#ff057e", "#690f54", "#bf6524", "#176b15")
basic_colors = pio.templates["draft"]["layout"]["colorway"]
pio.templates["draft"]["layout"]["colorway"] = basic_colors + additional_colors

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

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