Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Plotly - Align Y Axis for Scatter and Bar

Tags:

python

plotly

I'm trying to create a plotly graph with a Scatter and Graph elements. It all goes nicely, but one issue - the two Y axis don't align around 0.

I have tried playing with different attributes, such as 'mirror' and tick0, I also tried following the examples on plotly's site, but it's mostly multiple y-axis with the same graph type.

What can I do to fix this?

Image of the graph

import utils
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
import plotly




pd_data ['dt'] = ... dates
pd_data['price'] = ... prices
pd_data['car'] = ... cars

price = go.Scatter(
    x = pd_data['dt'],
    y = pd_data['price'],
    mode = 'lines',
    name = 'Price',
    xaxis = 'x',
    yaxis='y1',     
    marker = dict(
        color = utils.prep_color_string('orange'),
    ),
    line = dict(
        width = utils.line_width,
    ),
)

car = go.Bar(
    x = pd_data['dt'],
    y = pd_data['car'],
    #mode = 'lines',
    name = 'Cars',
    xaxis = 'x',
    yaxis='y2',
    marker = dict(
        color = utils.prep_color_string('light_green'),
    ),
    #line = dict(
    #   width = utils.line_width,
    #),
)

data = [price, car]

layout = dict(

    title = 'Price/Car',

    geo = dict(
        showframe = True,
        showcoastlines = True,
        projection = dict(
            type = 'Mercator'
        )
    ),

    yaxis=dict(
        title = 'Price',
        tickprefix = "$",
        overlaying='y2',
        anchor = 'x'            
    ),

    yaxis2=dict(
        title = 'Car',
        dtick = 1,
        #tickprefix = "",
        side = 'right',
        anchor = 'x',

    ),

)

fig = dict( data=data, layout=layout)
div = plotly.offline.plot( fig, validate=False, output_type = 'file',filename='graph.html' ,auto_open = False)
like image 559
EladA Avatar asked Mar 19 '16 12:03

EladA


People also ask

How do I change the Y-axis scale in Python?

To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do you plot multiple Y-axis in Python?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

How do you create a secondary axis in plotly?

First, import the necessary functions from the Plotly package and create the secondary axes using the specs parameter in the make_subplots() function as shown. Plot a scatter plot with multiple y-axes. Make the chart readable by adding titles to the x and y axes.


1 Answers

I have been struggling with this as well. Exact same problem, but I am using R. The way I figured around it was to use the rangemode="tozero" for both the yaxis and yaxis2 layouts.

I think in your case, it would look like this:

layout = dict(

    title = 'Price/Car',

    geo = dict(
        showframe = True,
        showcoastlines = True,
        projection = dict(
            type = 'Mercator'
        )
    ),

    yaxis=dict(
        title = 'Price',
        tickprefix = "$",
        overlaying='y2',
        anchor = 'x',
        rangemode='tozero'            
    ),

    yaxis2=dict(
        title = 'Car',
        dtick = 1,
        #tickprefix = "",
        side = 'right',
        anchor = 'x',
        rangemode = 'tozero'


    ),

)

Let me know if that works for you.

like image 161
R_User123456789 Avatar answered Sep 24 '22 01:09

R_User123456789