[英]Plotly (Python) scatter plot that shows a bubble for mean and standard deviation
我有對應於兩個不同變量 X 和 Y 的統計數據。例如:
x_stats = {'count': 100.0,
'mean': -0.19,
'std': 0.23,
'min': -0.67,
'25%': -0.38,
'50%': -0.15,
'75%': -0.02,
'max': 0.34}
y_stats = {'count': 100.0,
'mean': 0.34,
'std': 0.08,
'min': 0.15,
'25%': 0.28, # Q1
'50%': 0.34, # Q2
'75%': 0.38, # Q3
'max': 0.62}
單獨為每個變量獲取箱線圖很容易:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Box())
fig.update_traces(q1=[x_stats.get('25%'), y.get('25%')],
median=[x_stats.get('50%'), y.get('50%')],
q3=[x_stats.get('75%'), y_stats.get('75%')],
lowerfence=[x_stats.get('min'), y_stats.get('min')],
upperfence=[x_stats.get('max'), y_stats.get('max')],
mean=[x_stats.get('mean'), y_stats.get('mean')],
sd=[x_stats.get('std'), y_stats.get('std')],
)
fig.show()
但是,我想制作一個“散點圖”(實際上只是帶有一個橢圓的軸線),主要查看兩個變量的聯合分布(主要關注均值和標准差)。 橢圓的中心位於 (mean_x, mean_y),橢圓的軸位於 (std_x, std_y)。 那么 plot 看起來像這樣:
如何在 plotly express 中制作這樣的圖表?
import numpy as np
import matplotlib.pyplot as plt
x_stats = {'count': 100.0,
'mean': -0.19,
'std': 0.23,
'min': -0.67,
'25%': -0.38,
'50%': -0.15,
'75%': -0.02,
'max': 0.34}
y_stats = {'count': 100.0,
'mean': 0.34,
'std': 0.08,
'min': 0.15,
'25%': 0.28, # Q1
'50%': 0.34, # Q2
'75%': 0.38, # Q3
'max': 0.62}
plt.plot(
x_stats.get('mean') + x_stats.get('std') * np.cos(t),
y_stats.get('mean') + y_stats.get('std') * np.sin(t)
)
plt.grid(color='lightgray', linestyle='--')
plt.xlim([-1, 1])
plt.ylim([-1, 1])
plt.axvline(x=0, color='k')
plt.axhline(y=0, color='k')
plt.show()
然而,這不是 plotly。
我想到了
import plotly.graph_objects as go
fig = go.Figure()
fig.add_shape(type="circle",
xref="x", yref="y",
x0=x_stats['mean'] - x_stats['std'], y0=y_stats['mean']-y_stats['std'],
x1=x_stats['mean'] + x_stats['std'], y1=y_stats['mean']+y_stats['std'],
opacity=0.2,
fillcolor="blue",
line_color="blue",
)
fig.update_layout(showlegend=False)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.