python条形图y轴_Python绘图-将Y轴对齐以进行散点图和条形图

1586010002-jmsa.png

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?

rFPlr.png

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)

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值