[statistics] plots work for old and new matplotlib

This commit is contained in:
2020-10-27 23:28:30 +01:00
parent ca11914956
commit d1c6174ae2
6 changed files with 55 additions and 42 deletions

View File

@@ -2,7 +2,6 @@ import numpy as np
import matplotlib.pyplot as plt
from plotstyle import *
sketch_style()
# roll the die:
rng = np.random.RandomState(57281)
x1 = rng.randint(1, 7, 100)
@@ -27,9 +26,9 @@ ax2.set_xlabel('x')
ax2.set_ylim(0, 0.23)
ax2.set_ylabel('Probability')
ax2.plot([0.2, 6.8], [1.0/6.0, 1.0/6.0], zorder=-10, **lsAm)
try:
if mpl_major > 1:
ax2.hist([x2, x1], bins, density=True, zorder=-5, **fs)
except AttributeError:
else:
ax2.hist([x2, x1], bins, normed=True, zorder=-5, **fs)
fig.subplots_adjust(left=0.125)
fig.savefig('diehistograms.pdf')

View File

@@ -4,7 +4,6 @@ import matplotlib.gridspec as gridspec
from scipy.stats import gaussian_kde
from plotstyle import *
#sketch_style()
#rng = np.random.RandomState(981)
#data = rng.randn(40, 1) + 4.0
rng = np.random.RandomState(1981)
@@ -19,9 +18,11 @@ fig = plt.figure(figsize=cm_size(figure_width, 1.1*figure_height))
spec = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1], wspace=0.1,
**adjust_fs(fig, left=4.0))
ax = fig.add_subplot(spec[0, 0])
ax = fig.add_subplot(spec[0, 0], label='1')
wh = ax.boxplot( data, positions=[boxpos], widths=[barwidth], whis=100.0, patch_artist=True)
mediancolor = 'k'
wh['medians'][0].set_linewidth(4)
wh['medians'][0].set_color(mediancolor)
wh['whiskers'][0].set_linewidth(2)
wh['whiskers'][1].set_linewidth(2)
wh['whiskers'][0].set_linestyle('-')
@@ -34,6 +35,16 @@ wh['caps'][1].set_color(whiskercolor)
wh['boxes'][0].set_facecolor('#99ff00')
ax.set_xlim(0.0, 4.8)
ax.set_ylim( 0.0, 8.0)
ax.set_xticklabels([])
ax = fig.add_subplot(spec[0, 0], label='2')
ax.set_xlim(0.0, 4.8)
ax.set_xticks([scatterpos, barpos, boxpos])
ax.set_xticklabels(['(1) data', '(2) bar\n plot', '(3) box-\nwhisker'], fontsize='medium')
ax.set_ylabel('x')
ax.set_ylim( 0.0, 8.0)
# annotate box whisker:
ax.annotate('maximum',
xy=(boxpos, 6.5), xycoords='data',
xytext=(boxpos-1*barwidth, 7.6), textcoords='data', ha='left',
@@ -49,14 +60,6 @@ ax.annotate('median',
xytext=(boxpos+0.1*barwidth, 4.2), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0),
connectionstyle="angle3,angleA=-60,angleB=20") )
ax.set_xticklabels([])
ax = fig.add_subplot(spec[0, 0])
ax.set_xlim(0.0, 4.8)
ax.set_xticks([scatterpos, barpos, boxpos])
ax.set_xticklabels(['(1) data', '(2) bar\n plot', '(3) box-\nwhisker'], fontsize='medium')
ax.set_ylabel('x')
ax.set_ylim( 0.0, 8.0)
# scatter data points according to their density:
kernel = gaussian_kde(data)
@@ -67,7 +70,10 @@ ax.plot(scatterpos+barwidth*x*(rng.rand(len(data))-0.5), data, **psA)
barmean = np.mean(data)
barstd = np.std(data)
ew = 0.2
ax.bar([barpos-0.5*barwidth], [barmean], barwidth, **fsC)
if mpl_major > 1:
ax.bar([barpos], [barmean], barwidth, **fsC)
else:
ax.bar([barpos-0.5*barwidth], [barmean], barwidth, **fsC)
ax.plot([barpos, barpos], [barmean-barstd, barmean+barstd], **lsMarker)
ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean-barstd, barmean-barstd], **lsMarker)
ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean+barstd, barmean+barstd], **lsMarker)

View File

@@ -1,6 +1,7 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.ticker as ticker
from plotstyle import *
# normal distribution:
@@ -37,41 +38,39 @@ spec = gridspec.GridSpec(nrows=2, ncols=2, wspace=0.35, hspace=0.3,
**adjust_fs(fig, left=5.5, top=0.2, bottom=2.7))
ax = fig.add_subplot(spec[0, 0])
ax.set_xlabel('x')
ax.set_xlim(-3.2, 3.2)
ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.set_ylabel('p(x)')
ax.set_ylim(0.0, 0.49)
ax.set_yticks(np.arange(0.0, 0.41, 0.1))
#ax.plot(x, g, '-b', lw=2, zorder=-1)
try:
ax.yaxis.set_major_locator(ticker.MultipleLocator(0.2))
if mpl_major > 1:
ax.hist(r, np.arange(-4.1, 4, 0.4), density=True, zorder=-5, **fsC)
except AttributeError:
else:
ax.hist(r, np.arange(-4.1, 4, 0.4), normed=True, zorder=-5, **fsC)
ax = fig.add_subplot(spec[1, 0])
ax.set_xlabel('x')
ax.set_xlim(-3.2, 3.2)
ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.set_ylabel('p(x)')
ax.set_ylim(0.0, 0.49)
ax.set_yticks(np.arange(0.0, 0.41, 0.1))
#ax.plot(x, g, '-b', lw=2, zorder=-1)
try:
ax.yaxis.set_major_locator(ticker.MultipleLocator(0.2))
if mpl_major > 1:
ax.hist(r, np.arange(-4.3, 4, 0.4), density=True, zorder=-5, **fsC)
except AttributeError:
else:
ax.hist(r, np.arange(-4.3, 4, 0.4), normed=True, zorder=-5, **fsC)
ax = fig.add_subplot(spec[:, 1])
ax.set_xlabel('x')
ax.set_xlim(-3.2, 3.2)
ax.set_xticks(np.arange(-3.0, 3.1, 1.0))
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0))
ax.set_ylabel('Prob. density p(x)')
ax.set_ylim(0.0, 0.49)
ax.set_yticks(np.arange(0.0, 0.41, 0.1))
kd, xx = kerneldensity(r, -3.2, 3.2, 0.2)
ax.fill_between(xx, 0.0, kd, zorder=-5, **fsDs)
ax.plot(xx, kd, '-', zorder=-1, **lsB)
fig.subplots_adjust(left=0.15)
fig.savefig('kerneldensity.pdf')

View File

@@ -8,7 +8,7 @@ x = np.arange(-4.0, 4.0, 0.01)
g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi)
r = rng.randn(100)
fig, (ax1, ax2) = plt.subplots(1, 2)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=cm_size(figure_width, 1.1*figure_height))
ax1.set_xlabel('x')
ax1.set_xlim(-3.2, 3.2)
ax1.set_xticks(np.arange(-3.0, 3.1, 1.0))
@@ -24,10 +24,10 @@ ax2.set_ylabel('Probab. density p(x)')
ax2.set_ylim(0.0, 0.44)
ax2.set_yticks(np.arange(0.0, 0.41, 0.1))
ax2.plot(x, g, zorder=-1, **lsA)
try:
if mpl_major > 1:
ax2.hist(r, 5, density=True, zorder=-10, **fsB)
ax2.hist(r, 20, density=True, zorder=-5, **fsC)
except AttributeError:
else:
ax2.hist(r, 5, normed=True, zorder=-10, **fsB)
ax2.hist(r, 20, normed=True, zorder=-5, **fsC)

View File

@@ -10,7 +10,7 @@ x2=1.0
fig, ax = plt.subplots(figsize=cm_size(figure_width, 1.2*figure_height))
ax.set_xlabel('x')
ax.set_ylabel('Probability density p(x)')
ax.set_ylabel('Prob. density p(x)')
ax.set_ylim(0.0, 0.46)
ax.set_yticks(np.arange(0.0, 0.45, 0.1))
ax.annotate('Gaussian',