Home > Blockchain >  Plot average 'y' vs. 'x' (i.e., to plot single point fix 'x' and calcu
Plot average 'y' vs. 'x' (i.e., to plot single point fix 'x' and calcu

Time:09-30

I'd like to plot the average of 'y' against 'x' with the line thickness proportional to the sample size. The dataset has multiple y's for each value of x, and it has multiple different values of x. The reason I'd like to make this kind of plot is that I believe there is a linear relationship between average of 'y' and x, but that relationship breaks down when the sample size (i.e., the no of rows used to calculate the average of 'y') gets too small. Does this kind of plot have a name and is it possible to produce it in Matplotlib? Thank you.

CodePudding user response:

Matplotlib's enter image description here

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection as lc
import numpy as np

np.random.seed(20220929)

x0 = np.linspace(0, 100, 101)
x1 = np.empty(102)
x1[1:-1] = (x0[:-1] x0[1:])/2
x1[0] = x0[0] ; x1[-1] = x0[-1]

y0 = np.cumsum(np.random.random(150)-0.48)
for _ in range(49) : y0 = (y0[:-1] y0[1:])/2
y1 = np.empty(102)
y1[1:-1] = (y0[:-1] y0[1:])/2
y1[0] = y0[0] ; y1[-1] = y0[-1]

count = 1
for _ in range(10) : count  = np.random.randint(0,5, 101)
cmin, cmax = min(count), max(count)
dcount = cmax-cmin
ticcmin,  dticc= 0.5, 3
ticc = [ticcmin dticc*(c-cmin)/dcount for c in count]

segments = [((x1[i],y1[i]),(x0[i],y0[i]),(x1[i 1],y1[i 1])) for i in range(101)]
lines = lc(segments, linewidths=ticc)

fig, ax = plt.subplots()

ax.plot(x0, y0, lw=0.01)
ax.add_collection(lines)
plt.show()
  • Related