用 plotly express 叠加两个直方图
Overlaying two histograms with plotly express
我想使用以下简单代码叠加两个直方图,目前我只显示一个并排显示。这两个数据帧 长度不同 ,但叠加它们的直方图值仍然有意义。
import plotly.express as px
fig1 = px.histogram(test_lengths, x='len', histnorm='probability', nbins=10)
fig2 = px.histogram(train_lengths, x='len', histnorm='probability', nbins=10)
fig1.show()
fig2.show()
with pure plotly,就是这样,copy from the documentation:
import plotly.graph_objects as go
import numpy as np
x0 = np.random.randn(500)
# Add 1 to shift the mean of the Gaussian distribution
x1 = np.random.randn(500) + 1
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))
# Overlay both histograms
fig.update_layout(barmode='overlay')
# Reduce opacity to see both histograms
fig.update_traces(opacity=0.75)
fig.show()
我只是想知道有没有什么特别地道的 plotly express 方式。希望这也能说明 plotly 和 plotly express 之间的完整性和不同抽象级别。
诀窍是通过将数据组合成一个整洁的数据框来制作单个 Plotly Express 图,而不是制作两个图并尝试将它们组合(目前这是不可能的):
import numpy as np
import pandas as pd
import plotly.express as px
x0 = np.random.randn(250)
# Add 1 to shift the mean of the Gaussian distribution
x1 = np.random.randn(500) + 1
df =pd.DataFrame(dict(
series=np.concatenate((["a"]*len(x0), ["b"]*len(x1))),
data =np.concatenate((x0,x1))
))
px.histogram(df, x="data", color="series", barmode="overlay")
产量:
您可以获取 px 结构并使用它来创建图形。我希望使用 'color' 选项显示堆叠直方图,该选项很明确,但很难在纯情节中重新创建。
给定一个数据帧 (df),其中 utctimestamp 作为时间索引,严重性和类别作为直方图中要计算的内容,我用它来获得堆叠直方图:
figure_data=[]
figure_data.extend([i for i in px.histogram(df, x="utctimestamp", color="severity", histfunc="count").to_dict()['data']])
figure_data.extend([i for i in px.histogram(df, x="utctimestamp", color="category", histfunc="count").to_dict()['data']])
fig=go.Figure(figure_data)
fig.update_layout(barmode='stack')
fig.update_traces(overwrite=True, marker={"opacity": 0.7})
fig.show()
tl;dr px.histogram
创建一个直方图对象列表,您可以将其作为列表抓取并通过 go.Figure
.
呈现
我不能 post 内联,但这里是 px https://imgur.com/a/l7BblZo
的堆叠直方图
如果想使用 plotly 的 graph_objects
模块,可以改为使用 barmode="overlay"
,如下所示用于 2 个直方图。
import plotly.graph_objects as go
fig = go.Figure(data=[go.Histogram(x=x)])
fig.add_trace(go.Histogram(x=x,))
fig.update_layout(barmode='overlay')
我想使用以下简单代码叠加两个直方图,目前我只显示一个并排显示。这两个数据帧 长度不同 ,但叠加它们的直方图值仍然有意义。
import plotly.express as px
fig1 = px.histogram(test_lengths, x='len', histnorm='probability', nbins=10)
fig2 = px.histogram(train_lengths, x='len', histnorm='probability', nbins=10)
fig1.show()
fig2.show()
with pure plotly,就是这样,copy from the documentation:
import plotly.graph_objects as go
import numpy as np
x0 = np.random.randn(500)
# Add 1 to shift the mean of the Gaussian distribution
x1 = np.random.randn(500) + 1
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))
# Overlay both histograms
fig.update_layout(barmode='overlay')
# Reduce opacity to see both histograms
fig.update_traces(opacity=0.75)
fig.show()
我只是想知道有没有什么特别地道的 plotly express 方式。希望这也能说明 plotly 和 plotly express 之间的完整性和不同抽象级别。
诀窍是通过将数据组合成一个整洁的数据框来制作单个 Plotly Express 图,而不是制作两个图并尝试将它们组合(目前这是不可能的):
import numpy as np
import pandas as pd
import plotly.express as px
x0 = np.random.randn(250)
# Add 1 to shift the mean of the Gaussian distribution
x1 = np.random.randn(500) + 1
df =pd.DataFrame(dict(
series=np.concatenate((["a"]*len(x0), ["b"]*len(x1))),
data =np.concatenate((x0,x1))
))
px.histogram(df, x="data", color="series", barmode="overlay")
产量:
您可以获取 px 结构并使用它来创建图形。我希望使用 'color' 选项显示堆叠直方图,该选项很明确,但很难在纯情节中重新创建。
给定一个数据帧 (df),其中 utctimestamp 作为时间索引,严重性和类别作为直方图中要计算的内容,我用它来获得堆叠直方图:
figure_data=[]
figure_data.extend([i for i in px.histogram(df, x="utctimestamp", color="severity", histfunc="count").to_dict()['data']])
figure_data.extend([i for i in px.histogram(df, x="utctimestamp", color="category", histfunc="count").to_dict()['data']])
fig=go.Figure(figure_data)
fig.update_layout(barmode='stack')
fig.update_traces(overwrite=True, marker={"opacity": 0.7})
fig.show()
tl;dr px.histogram
创建一个直方图对象列表,您可以将其作为列表抓取并通过 go.Figure
.
我不能 post 内联,但这里是 px https://imgur.com/a/l7BblZo
的堆叠直方图如果想使用 plotly 的 graph_objects
模块,可以改为使用 barmode="overlay"
,如下所示用于 2 个直方图。
import plotly.graph_objects as go
fig = go.Figure(data=[go.Histogram(x=x)])
fig.add_trace(go.Histogram(x=x,))
fig.update_layout(barmode='overlay')