2016-03-19 7 views
2

Ich versuche ein Plotly-Diagramm mit Scatter- und Graph-Elementen zu erstellen. Es geht alles gut, aber ein Problem - die zwei Y-Achse nicht um 0.Python Plotly - Y-Achse für Scatter und Bar ausrichten

Ich habe versucht zu spielen mit verschiedenen Attributen, wie "Spiegel" und Tick0, ich habe auch versucht, die Beispiele auf Plotly Website folgen, aber es ist meistens mehrere Y-Achsen mit dem gleichen Graphen-Typ.

Was kann ich tun, um das Problem zu beheben?

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

Wenn Sie einen MCVE bereitstellen können, erhalten Sie schneller eine gute Antwort. http://StackOverflow.com/Help/Mcve – steven

Antwort

5

ich kämpfen mit diesem als auch haben. Genau dasselbe Problem, aber ich benutze R. Die Art und Weise, wie ich es umging, war die Verwendung von rangemode = "tozero" für die Layouts yaxis und yaxis2.

Ich denke, in Ihrem Fall, es würde wie folgt aussehen:

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' 


    ), 

) 

Lassen Sie mich wissen, ob das für Sie arbeitet.

+0

Dies funktionierte auch für plotly.js – jurasource

+1

Dies hat für mich versucht, 0 auf mehreren Y-Achse auszurichten –