简体   繁体   中英

Fix the scale/size of the y axis

I am using Plotly to display some graph for a website I am building.

The user can answer some questions and the graph display the percentage "questions answered / questions not answered".

I used both a Bar char and a Radar chart to show visually such percentage. (This percentage ranges from 0 to 1).

However, the y axis is never fixed with a scale from 0 to 1, but from 0 to the highest percentage answered.

For example:

If the user answers 70% of the questions, the y axis shows the maximum is 0.7 (70%) not 1 (100%).

I want the y axis scale to be always 1 (100%) rather than adjusting to what the user has answered.

This occurs also for the Radar chart.

My code:

  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
      )
    )

For the bar chart you can add:

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

here:

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
    )

For the radar chart you can add:

range = [0, 1]

Something like:

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

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

If you want to set the limit on the Y-Axis, use:

plt.ylim(0,1)

edit, with plotly this should do it::

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

Documentation: https://plot.ly/python/axes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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