Python Plotly - 为散点图和条形图对齐 Y 轴 [英] Python Plotly - Align Y Axis for Scatter and Bar

查看:26
本文介绍了Python Plotly - 为散点图和条形图对齐 Y 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Scatter 和 Graph 元素创建一个绘图图.一切都很顺利,但有一个问题 - 两个 Y 轴不围绕 0 对齐.

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.

我尝试过使用不同的属性,例如 'mirror' 和 tick0,我也尝试按照 plotly 网站上的示例进行操作,但它大多是具有相同图形类型的多个 y 轴.

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?

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)

推荐答案

我也一直在努力解决这个问题.完全相同的问题,但我使用的是 R.我想到的方法是对 yaxis 和 yaxis2 布局都使用 rangemode="tozero".

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.

这篇关于Python Plotly - 为散点图和条形图对齐 Y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆