ministep / SQL_DataAnalysis

SQL数据分析
9 stars 0 forks source link

plotly 画双轴 #70

Closed kemistep closed 4 years ago

kemistep commented 4 years ago

结果图示 image

import numpy as np
import plotly.graph_objects as go

df_v1 = df_warning
df_v1['ds'] = pd.DatetimeIndex(df_v1.index).strftime("%Y-%m-%d")

x = 'ds'
y1 = '活跃用户数'
y2 = '活跃用户数_pop_rate'
#Create traces
trace1 = go.Bar(x= df_v1[x].values.tolist(),
                    y=df_v1[y1].values.tolist(),
                    #mode='lines+markers',
                    name='大数值展示',
                    )

trace2 = go.Scatter(x=df_v1[x].values.tolist(), 
                    y=df_v1[y2].values.tolist(),
                    mode='lines+markers',
                    name='小数值展示',
                    yaxis="y2")

data1 = [trace1, trace2]

# go.Layout可以创建图层对象,实现双坐标
layout = go.Layout(title="双坐标示例图",
                   xaxis = dict(tickformat="%Y-%m-%d"),
                   yaxis=dict(title="大数值"),
                   yaxis2=dict(title="小数值",overlaying='y', side="right"),
                   legend=dict(x=0, y=1, font=dict(size=12, color="black")))
fig = go.Figure(data=data1, layout=layout)
fig.show()
kemistep commented 4 years ago

x轴坐标轴,日期显示设置:xaxis = dict(tickformat="%Y-%m-%d")

kemistep commented 4 years ago

参考资料:How to make the messy date ticks organized - Dash - Plotly Community Forum]

kemistep commented 4 years ago

其他参考: 可视化神器plotly(1):基础图表与画布、坐标轴 - 古明地盆 - 博客园] Plotly-express-16-绘制技巧(一) - 掘金]