Python Plotly - 为散点图和条形图对齐 Y 轴

2023-09-28Python开发问题
43

本文介绍了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 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10