简体   繁体   English

根据 plotly 中的值自定义条形图的颜色

[英]customize color of bar chart based on value in plotly

I am using plotly go figure instead of plotly express.我正在使用 plotly go 图而不是 plotly 快递。 so I use go.Bar(), How to color the bar based on the value?所以我使用 go.Bar(),如何根据值对条形图着色?

I am thinking to loop over one value at a time to plot one bar for one value such that different bar will have different colors.我正在考虑一次将一个值循环到 plot 一个条形对应一个值,这样不同的条形将具有不同的 colors。 Now the question is how to customize the color based on value, for example, from small value to larger value use colors from blue to red.现在的问题是如何根据值自定义颜色,例如从小值到大值使用 colors 从蓝色到红色。

Here is what I did for subplot case:这是我为子情节案例所做的:

    for ix_loc,loc in enumerate(time_plot_location_list):
        fig.add_trace(
            go.Bar(
            x=[time_plot_location_list[ix_loc]],   # single data have to use list [data]
            y=[slope_list[ix_loc]],  
            text=slope_list[ix_loc],
            hoverinfo='text',
            ),
            row=row,
            col=1,
                     )

在此处输入图像描述 Thank you so much for your help非常感谢你的帮助

Sometimes, while looking at Plotly documentation we can see examples that are easily achievable with Plotly express but "undocumented" on plotly.graph_objects .有时,在查看Plotly 文档时,我们可以看到使用 Plotly 快速实现但在plotly.graph_objects上“未记录”的示例。 In these occasions we can execute the example with plotly express, and explore what's going on with fig.data[0] (for example).在这些情况下,我们可以使用 plotly express 执行示例,并探索fig.data[0]的情况(例如)。 Then, we can replicate the options on regular plotly.graph_objects .然后,我们可以复制常规plotly.graph_objects上的选项。

Here is how you achieve it:以下是实现它的方法:

import plotly.graph_objects as go
import numpy as np

x = np.arange(20)
y = np.random.rand(len(x)) * 10
fig = go.Figure(data=[
    go.Bar(
        x=x, y=y,
        marker=dict(
            color=np.cos(x / 3),
            colorscale="Bluered_r",
            showscale=True,
            colorbar=dict(title="value"),
        ),
    )
])

在此处输入图像描述

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

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