【问题标题】:Plotly - Adjustbale bar plotPlotly - Adjustbale 条形图
【发布时间】:2021-12-28 22:15:24
【问题描述】:

我在 Plotly 中创建一个图表作为 Django 项目的一部分,我遇到的问题是,当我只有一个值时,很明显,只要我有多个项目,条的大小就会非常大,栏开始看起来不错,但是什么时候只有一个值真的很丑,有人知道如何解决这个问题吗?

def get_leads_data(request):
    c = Profile.objects.get(user=request.user)

    qs = Leads.objects.filter(
            agent_id=c,status='Open')

    df = read_frame(qs)

    graphs = []
    graphs.append(go.Bar(
            x=df['company'],
            y=df['expected_revenue'],
            name='Estimated Revenue'
    ))
    layout={
        'title': 'Estimated Revenue by Company',
        'xaxis_title':'Company',
        'yaxis_title':'Revenue',
        'height':500,
        'width':640,
    }
    plot_div = plot({'data': graphs, 'layout': layout},
                    output_type='div')
    return render(request,'account/plot.html',{'plot_div':plot_div})


【问题讨论】:

    标签: python django plotly


    【解决方案1】:

    提问者:

    def plot_deals_data(qs): df = read_frame(qs)

    graphs = []
     
    graphs.append(go.Bar(
        x=df['company'],
        y=df['revenue'],
        name='Estimated Revenue',
        marker=dict(color='#008375'),
        width=0.2,
    
    ))
    layout = {
        'title': 'Revenue by Company',
        'title_xanchor':'center',
        'title_yanchor':'top',
        'title_y':0.9,
        'title_x':0.5,
        'xaxis_title': 'Company',
        'yaxis_title': 'Actual Revenue',
        'height': 600,
        'width': 740,
        'font_family':'Muli',
        'font_color':'#008375',
        'font_size': 16
    }
    
    
    plot_div = plot({'data': graphs, 'layout': layout},
                    output_type='div',include_plotlyjs=False,
                    show_link=False,link_text="")
    
    return plot_div
    

    【讨论】:

      【解决方案2】:

      你可以设置go.Barwidth属性

      属性宽度

      设置误差线两端横杆的宽度(以 px 为单位)。

      “width”属性是一个数字,可以指定为: 区间 [0, inf] 中的 int 或 float

      示例用法

      import plotly.graph_objects as go
      
      fig = go.Figure(go.Bar(x=[1], y=[200], width=0.2))
      fig.show()
      

      由于您指定不喜欢只有一个条的宽度,您可以有条件地将值传递给width 属性。因此,如果您的数据的行数等于 1,您可以将宽度更改为更小,否则您不会传递任何内容并将其留给 plotly。

      【讨论】:

        猜你喜欢
        • 2021-11-28
        • 2018-01-06
        • 2020-08-25
        • 1970-01-01
        • 2016-02-25
        • 1970-01-01
        • 2016-11-14
        • 2018-12-07
        • 2021-11-04
        相关资源
        最近更新 更多