简体   繁体   English

Plotly (Python) 散点图 plot 显示平均值和标准差的气泡

[英]Plotly (Python) scatter plot that shows a bubble for mean and standard deviation

I have statistics that correspond to two different variables, X and Y. For example:我有对应于两个不同变量 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}

It's easy enough to get a boxplot for each variable individually:单独为每个变量获取箱线图很容易:

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()

However, I'd like to make a "scatter plot" (really just the axes lines with one ellipse) that looks primarily at the joint distribution of the two variables (focusing mainly on the means and standard deviations).但是,我想制作一个“散点图”(实际上只是带有一个椭圆的轴线),主要查看两个变量的联合分布(主要关注均值和标准差)。 The center of the ellipse would be at (mean_x, mean_y) and the axes of the ellipse would be (std_x, std_y).椭圆的中心位于 (mean_x, mean_y),椭圆的轴位于 (std_x, std_y)。 So then the plot would look something like this:那么 plot 看起来像这样:

How can I make such a graph in plotly express?如何在 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()

椭圆

However, that's not with plotly.然而,这不是 plotly。

I figured it out我想到了

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM