<small id='Qod7l'></small><noframes id='Qod7l'>

    <i id='Qod7l'><tr id='Qod7l'><dt id='Qod7l'><q id='Qod7l'><span id='Qod7l'><b id='Qod7l'><form id='Qod7l'><ins id='Qod7l'></ins><ul id='Qod7l'></ul><sub id='Qod7l'></sub></form><legend id='Qod7l'></legend><bdo id='Qod7l'><pre id='Qod7l'><center id='Qod7l'></center></pre></bdo></b><th id='Qod7l'></th></span></q></dt></tr></i><div id='Qod7l'><tfoot id='Qod7l'></tfoot><dl id='Qod7l'><fieldset id='Qod7l'></fieldset></dl></div>
  1. <tfoot id='Qod7l'></tfoot>
    <legend id='Qod7l'><style id='Qod7l'><dir id='Qod7l'><q id='Qod7l'></q></dir></style></legend>

      • <bdo id='Qod7l'></bdo><ul id='Qod7l'></ul>
    1. Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?

      时间:2023-09-28

      <small id='j3eqF'></small><noframes id='j3eqF'>

      <tfoot id='j3eqF'></tfoot>
      • <bdo id='j3eqF'></bdo><ul id='j3eqF'></ul>
          <tbody id='j3eqF'></tbody>
          <i id='j3eqF'><tr id='j3eqF'><dt id='j3eqF'><q id='j3eqF'><span id='j3eqF'><b id='j3eqF'><form id='j3eqF'><ins id='j3eqF'></ins><ul id='j3eqF'></ul><sub id='j3eqF'></sub></form><legend id='j3eqF'></legend><bdo id='j3eqF'><pre id='j3eqF'><center id='j3eqF'></center></pre></bdo></b><th id='j3eqF'></th></span></q></dt></tr></i><div id='j3eqF'><tfoot id='j3eqF'></tfoot><dl id='j3eqF'><fieldset id='j3eqF'></fieldset></dl></div>

          <legend id='j3eqF'><style id='j3eqF'><dir id='j3eqF'><q id='j3eqF'></q></dir></style></legend>

              • 本文介绍了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 对绘图对象条形图进行颜色编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在谷歌协作笔记本中显示输出? 下一篇:如何更改我的 Dash Graph 的大小?

                相关文章

                <small id='oVskz'></small><noframes id='oVskz'>

              • <legend id='oVskz'><style id='oVskz'><dir id='oVskz'><q id='oVskz'></q></dir></style></legend>

              • <i id='oVskz'><tr id='oVskz'><dt id='oVskz'><q id='oVskz'><span id='oVskz'><b id='oVskz'><form id='oVskz'><ins id='oVskz'></ins><ul id='oVskz'></ul><sub id='oVskz'></sub></form><legend id='oVskz'></legend><bdo id='oVskz'><pre id='oVskz'><center id='oVskz'></center></pre></bdo></b><th id='oVskz'></th></span></q></dt></tr></i><div id='oVskz'><tfoot id='oVskz'></tfoot><dl id='oVskz'><fieldset id='oVskz'></fieldset></dl></div>
                  <bdo id='oVskz'></bdo><ul id='oVskz'></ul>
                <tfoot id='oVskz'></tfoot>