【问题标题】:Fix the scale/size of the y axis固定y轴的比例/大小
【发布时间】:2019-12-30 15:51:35
【问题描述】:

我正在使用 Plotly 为我正在构建的网站显示一些图表。

用户可以回答一些问题,图表显示“已回答问题/未回答问题”的百分比。

我同时使用了条形图和雷达图来直观地显示这样的百分比。 (这个百分比范围从 0 到 1)。

但是,y 轴从不固定,范围从 0 到 1,而是从 0 到回答的最高百分比。

例如:

如果用户回答了 70% 的问题,则 y 轴显示最大值为 0.7 (70%) 而不是 1 (100%)。

我希望 y 轴刻度始终为 1 (100%),而不是根据用户的回答进行调整。

雷达图也会出现这种情况。

我的代码:

  fig2 = go.Figure()
  fig2.add_trace(go.Bar(
      x=categories,
      y=[a1, b1, c1, d1, e1],
      name='You',
      marker_color='#A5A9F7'
  ))
  fig2.add_trace(go.Bar(
      x=categories,
      y=[a2, b2, c2, d2, e2],
      name='Other',
      marker_color='#E89C8C'
  ))
  fig2.update_layout(
      title=go.layout.Title(
          text="<b>This graph show the percentage (0 to 1)",
          font=dict(size=10),
          xref="paper",
          x=0
      )
    )

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    如果要设置 Y 轴的限制,请使用:

    plt.ylim(0,1)
    

    编辑,用情节这应该做到这一点::

    fig2.update_yaxes(range=[0, 1])
    

    文档:https://plot.ly/python/axes/

    【讨论】:

    • 你能告诉我在我的代码中你会在哪里做吗?
    • 最后你可能有类似fig2.show的东西,就在使用fig2.update_yaxes(range=[0, 1])之前
    【解决方案2】:

    对于条形图,您可以添加:

    yaxis=dict( range=[0, 1] )

    这里:

    fig2 = go.Figure()
      fig2.add_trace(go.Bar(
          x=categories,
          y=[a1, b1, c1, d1, e1],
          name='You',
          marker_color='#A5A9F7'
      ))
      fig2.add_trace(go.Bar(
          x=categories,
          y=[a2, b2, c2, d2, e2],
          name='Other',
          marker_color='#E89C8C'
      ))
      fig2.update_layout(
          title=go.layout.Title(
              text="<b>This graph show the percentage (0 to 1)",
              font=dict(size=10),
              xref="paper",
              x=0
          ),
        yaxis=dict( # Here
            range=[0, 1] # Here
        ) # Here
        )
    

    对于雷达图,您可以添加:

    range = [0, 1]
    

    类似:

    layout = go.Layout(
      polar = dict(
        radialaxis = dict(
          visible = True,
          range = [0, 50]
        )
      ),
      showlegend = False
    )
    

    https://plot.ly/pandas/radar-chart/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多