Plotly: Change y-axis scale

a125

I have a dataset that looks like this:

    x       y           z
0   Jan     28446000    110489.0    
1   Feb     43267700    227900.0    

When I plot a line chart like this:

px.line(data,x = 'x', y = ['y','z'], line_shape = 'spline', title="My Chart")

The y axis scale comes from 0 to 90 M. The first line on the chart for y is good enough. However, the second line appears to be always at 0M. What can I do to improve my chart such that we can see clearly how the values of both column change over the x values?

Is there any way I can normalize the data? Or perhaps I could change the scaling of the chart.

S3DEV

Often times we use data which is in different scales, and scaling the data would mask a characteristic we wish to display. One way to handle this is to add a secondary y-axis. An example is shown below.

Key points:

  • Create a layout dictionary object
  • Add a yaxis2 key to the dict, with the following: 'side': 'right', 'overlaying': 'y1'
    • This tells Plotly to create a secondary y-axis on the right side of the graph, and to overlay the primary y-axis.
  • Assign the appropriate trace to the newly created secondary y-axis as: 'yaxis': 'y2'
    • The other trace does not need to be assigned, as 'y1' is the default y-axis.

Comments (TL;DR):
The example code shown here uses the lower-level Plotly API, rather than a convenience wrapper such as graph_object to express. The reason is that I (personally) feel it's helpful to users to show what is occurring 'under the hood', rather than masking the underlying code logic with a convenience wrapper.

This way, when the user needs to modify a finer detail of the graph, they will have a better understanding of the lists and dicts which Plotly is constructing for the underlying graphing engine (orca).

The Docs:
Here is a link to the Plotly docs referencing multiple axes.

Example Code:

import pandas as pd
from plotly.offline import iplot

df = pd.DataFrame({'x': ['Jan', 'Feb'],
                   'y': [28446000, 43267700],
                   'z': [110489.0, 227900.0]})

layout = {'title': 'Secondary Y-Axis Demonstration',
          'legend': {'orientation': 'h'}}

traces = []
traces.append({'x': df['x'], 'y': df['y'], 'name': 'Y Values'})
traces.append({'x': df['x'], 'y': df['z'], 'name': 'Z Values', 'yaxis': 'y2'})


# Add config common to all traces.
for t in traces:
    t.update({'line': {'shape': 'spline'}})

layout['yaxis1'] = {'title': 'Y Values', 'range': [0, 50000000]}
layout['yaxis2'] = {'title': 'Z Values', 'side': 'right', 'overlaying': 'y1', 'range': [0, 400000]}


iplot({'data': traces, 'layout': layout})

Graph:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plotly, same scale for x and y axis

Change format of y axis in plotly

change y axis scale in ggplot

How to dynamically change the scale/ticks of y axis in plotly charts upon zooming in?

plotly express | change tick units on y axis

How to change the Scale of Y-Axis in R

Add Currency symbol in the y axis scale using plotly method in python

Python Plotly Line Chart with a Dataframe: Wrong scale on the y-axis

Interactively change axis scale (linear/log) in Plotly image using R

How to change the x-axis and y-axis labels in plotly?

How to change the scale of my matplotlib y axis to y^2?

Change y-axis title hover in histogram plotly python

Change y-axis tick values of a heatmap with R/Plotly

Plotly: How to change the range of the y-axis of a subplot?

How to change point separator for the y-axis of a Plotly figure?

Change subplot y axis text via dropdown selection in plotly

ggplot - change y-axis label and scale manually

How to change y-axis scale in R boxplot function

How to change the y-axis scale in plot for a forecast object?

How to change y-axis to log-scale?

How to change y-axis in 'plotyy' to natural log scale in matlab

How to change scale of y-axis on timelion charts

How to change the y axis scale of ggsurvplot with very few events

Axis scale will not change with matplotlib

In an animated plotly scatter plot, how to change the axis scale per frame... but in R? (adapt a python solution?)

Plotly: multiple x and y axis

Change axis of plotly polar plot

How to change axis features in plotly?

Fix the scale/size of the y axis