Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?

Plotly: How to colorcode plotly graph objects bar chart using Python?(Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?)

本文介绍了Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def update_graph_bar(named_count,**kwargs):

traces = list()
df = pd.DataFrame(list(Message.objects.all().values()))
available_indicators = list(df['content'].unique())
for t in available_indicators:
    traces.append(go.Bar(
        x=[t],
        y=[df[df['content']==t]['timestamp'].count()],
        name='{}'.format(t),text=[df[df['content']==t]['timestamp'].count()],
        textposition='auto'
        ))
layout = plotly.graph_objs.Layout(barmode='group',paper_bgcolor='#00FFFF',
    plot_bgcolor='rgba(0,0,0,0)',)
return {'data': traces,
      'layout': layout}

我有上面的代码,在这里我想介绍使用标记"的颜色编码,这样条形图的颜色应该取决于它的值.随着值的增加,颜色也应该改变.

I have the above code and here I want to introduce colorcoding using 'marker' in such a way that the color of bargraph should be dependent on its value. as the value increases the color should also change.

推荐答案

我假设你正在寻找这样的东西:

I'm assuming you're looking for something like this:

情节1:情节表达和

可以这样轻松制作:

import plotly.express as px
data = px.data.gapminder()

data_canada = data[data.country == 'Canada']
fig = px.bar(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
             labels={'pop':'population of Canada'}, height=400)
fig.show()

您可以轻松地将该方法应用于 plotly.graph_objects 以获取:

You can easily adapt that approach to plotly.graph_objects to get:

情节 2: go.Bar()'viridis'

代码 2:

import plotly.graph_objects as go

fig = go.Figure()

x=[1,2,3]
y=[4,5,6]
z=[12,24,48]

fig.add_trace(go.Bar(x=x, y=y,
                     marker=dict(color = z,
                     colorscale='viridis')))

fig.show()

您甚至可以应用自己的自定义色阶:

And you can even apply your own custom color scale:

情节 3: 自定义颜色

代码 3:

import plotly.graph_objects as go

fig = go.Figure()

x=[1,2,3]
y=[4,5,6]
z=[12,24,48]

customscale=[[0, "rgb(255, 0, 0)"],
            [0.1, "rgb(255, 0, 0)"],
            [0.9, "rgb(0, 0, 255)"],
            [1.0, "rgb(0, 0, 255)"]]

fig.add_trace(go.Bar(x=x, y=y,
                     marker=dict(color = z,
                     colorscale=customscale)))

fig.show()

code 3 将颜色映射到变量的相对大小,code 4 将向您展示如何将颜色映射到具有指定阈值的绝对值:

While code 3 maps colors to relative sizes of a variable, code 4 will show you how you can map colors to absolute values with specified thresholds:

图 4:由变量的绝对值分配的颜色

Plot 4: Colors assigned by absolute values of a variable

代码 4:

import plotly.graph_objects as go

fig = go.Figure()

x=[1,2,3]
y=[25,75, 110]
z=[12,24,48]

def SetColor(y):
        if(y >= 100):
            return "red"
        elif(y >= 50):
            return "yellow"
        elif(y >= 0):
            return "green"

fig.add_trace(go.Bar(x=x, y=y,
                     marker=dict(color = list(map(SetColor, y)))))

fig.show()

这篇关于Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?

基础教程推荐