0

I have statistics that correspond to two different variables, X and Y. For example:

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). So then the plot would look something like this:

How can I make such a graph in plotly express?

acciolurker
  • 429
  • 4
  • 15

2 Answers2

1

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()
acciolurker
  • 429
  • 4
  • 15
0
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()

ellipse

However, that's not with plotly.

Michael Hodel
  • 2,845
  • 1
  • 5
  • 10