python - 在 2 个不同的 plotly 数据帧直方图上共享相同的 x 轴

标签 python pandas plotly

我想为 2 个不同的 pandas 数据帧绘制两个直方图,共享相同的 x 轴,使用 plotly 袖扣(或者如果不可能,只是 plotly)

以下代码绘制了 2 个不同的图,不共享 x 轴

 df1.iplot(kind='histogram')
 df2.iplot(kind='histogram')

我希望两个直方图共享相同的 x 轴

最佳答案

我想你在找这个:

import plotly.offline as py
import plotly.graph_objs as go

import numpy as np

x0 = np.random.randn(500)
x1 = np.random.randn(500)+1

trace1 = go.Histogram(
    x=x0,
    opacity=0.75
)
trace2 = go.Histogram(
    x=x1,
    opacity=0.75
)

data = [trace1, trace2]
layout = go.Layout(barmode='overlay')
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='overlaid histogram')

输出:

histograms

或者您可以使用共享的 x 轴创建子图:

from plotly import tools
import plotly.offline as py
import numpy as np
import plotly.graph_objs as go

trace1 = go.Histogram(
    x=np.random.randn(500)
)
trace2 = go.Histogram(
    x=np.random.randn(500)
)

fig = tools.make_subplots(rows=2, cols=1, specs=[[{}], [{}]],
                          shared_xaxes=True, shared_yaxes=False,
                          vertical_spacing=0.001)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 1, 1)

fig['layout'].update(height=600, width=600, title='Stacked Subplots with Shared X-Axes')
py.iplot(fig, filename='stacked-subplots-shared-xaxes')

输出:

subplots

关于python - 在 2 个不同的 plotly 数据帧直方图上共享相同的 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537240/

相关文章:

python - 使用不同数量的变量进行查询

python - 在上下文中评估变量

python - 减小 pyinstaller exe 的大小

javascript - 在不重绘背景图的情况下以交互方式向 plotly R 中的子图添加点

python - 如何在 Python 中创建 ramdisk?

python pandas 右填充值

Python Pandas 数据帧 : how to get complete dates for all categories

R plotly choropleth禁用鼠标滚轮缩放

javascript - 在 plotly 中更改特定点的颜色

python - 在Model的外部类方法中访问peewee的Meta子类