Examples - GooseMPL

Patch: plot finite element mesh

../../_images/patch.svg

[source: patch.py]


import matplotlib.pyplot as plt
import GooseMPL          as gplt
import numpy             as np

from mpl_toolkits.axes_grid1 import make_axes_locatable

plt.style.use(['goose','goose-latex'])

coor = np.array([
  [ 0.0, 0.0 ],
  [ 1.0, 0.0 ],
  [ 2.0, 0.0 ],
  [ 0.0, 1.0 ],
  [ 1.0, 1.0 ],
  [ 2.0, 1.0 ],
])

conn = np.array([
  [ 0, 1, 4, 3 ],
  [ 1, 2, 5, 4 ],
])

value = np.array([
  -1,
  +1,
])

fig,ax = plt.subplots()

im = gplt.patch(coor=coor,conn=conn,cindex=value,clim=[-2,2],cmap='RdBu_r')

plt.xlim([ -0.1,  2.1 ])
plt.ylim([ -0.1,  1.1 ])

ax.set_aspect('equal')

div  = make_axes_locatable(ax)
cax  = div.append_axes("right", size="5%", pad=0.1)
cbar = plt.colorbar(im,cax=cax)

plt.savefig('patch.svg')
plt.show()

Histogram

Histogram

../../_images/histogram.svg

[source: histogram.py]


import matplotlib.pyplot as plt
import GooseMPL          as gplt
import numpy             as np

plt.style.use(['goose','goose-latex'])

# ------------------------------------------------------------------------------

def distribution(a=100, b=3, g=-.3, size=10000):

  r = np.random.random(size=size)

  return (a**g + (b**g - a**g)*r)**(1./g)

# ------------------------------------------------------------------------------

data = distribution()

fig, axes = plt.subplots(ncols=3, nrows=2, figsize=(3*8,2*6))

# --- histogram ---

bin_edges = gplt.histogram_bin_edges(data, bins=41)

P, x = gplt.histogram(data, bins=bin_edges, density=False)

gplt.hist(P, x, facecolor=[.2,.2,.2], axis=axes[0,0])

P, x = gplt.histogram(data, bins=bin_edges, density=True)

gplt.hist(P, x, facecolor=[.2,.2,.2], axis=axes[1,0])

# --- histogram_log ---

bin_edges = gplt.histogram_bin_edges(data, bins=41, mode='log')

P, x = gplt.histogram(data, bins=bin_edges, density=False)

gplt.hist(P, x, facecolor='b', axis=axes[0,1])

P, x = gplt.histogram(data, bins=bin_edges, density=True)

gplt.hist(P, x, facecolor='b', axis=axes[1,1])

# --- histogram_uniform ---

bin_edges = gplt.histogram_bin_edges(data, bins=41, mode='uniform')

P, x = gplt.histogram(data, bins=bin_edges, density=False)

gplt.hist(P, x, facecolor='r', axis=axes[0,2])

P, x = gplt.histogram(data, bins=bin_edges, density=True)

gplt.hist(P, x, facecolor='r', axis=axes[1,2])

# --- axes settings ---

axes[0,0].set_title(r'histogram')
axes[0,1].set_title(r'histogram\_log')
axes[0,2].set_title(r'histogram\_uniform')

for ax in axes.ravel():

  ax.set_xlabel(r'$x$')
  ax.set_ylabel(r'$N(x)$')

plt.savefig('histogram.svg')
plt.show()

Probability density

../../_images/histogram_powerlaw.svg

[source: histogram_powerlaw.py]


import matplotlib.pyplot as plt
import GooseMPL          as gplt
import numpy             as np

plt.style.use(['goose','goose-latex'])

# ------------------------------------------------------------------------------

def distribution(a=100, b=3, g=-.3, size=10000):

  r = np.random.random(size=size)

  return (a**g + (b**g - a**g)*r)**(1./g)

# ------------------------------------------------------------------------------

data = distribution()

fig, axes = gplt.subplots(ncols=3)

# configure axes

for ax in axes:

  ax.set_xlabel(r'$x$')
  ax.set_ylabel(r'$\rho(x)$')

  ax.set_xscale('log')
  ax.set_yscale('log')

  ax.set_xlim([1e+0, 1e+3])
  ax.set_ylim([1e-3, 1e+0])

# histogram

bin_edges = gplt.histogram_bin_edges(data, bins=41)

P, x = gplt.histogram(data, bins=bin_edges, density=True, return_edges=False)

axes[0].plot(x,P,marker='o', linestyle='none', markersize=5., color='k')

# histogram_log

bin_edges = gplt.histogram_bin_edges(data, bins=41, mode='log')

P, x = gplt.histogram(data, bins=bin_edges, density=True, return_edges=False)

axes[1].plot(x,P,marker='o', linestyle='none', markersize=5., color='b')

# histogram_uniform

bin_edges = gplt.histogram_bin_edges(data, bins=41, mode='uniform')

P, x = gplt.histogram(data, bins=bin_edges, density=True, return_edges=False)

axes[2].plot(x,P,marker='o', linestyle='none', markersize=5., color='r')

# add titles

axes[0].set_title(r'histogram')
axes[1].set_title(r'histogram\_log')
axes[2].set_title(r'histogram\_uniform')

# annotate powerlaw in different ways

for ax in axes:

  gplt.grid_powerlaw(exp=-1.3, axis=ax)

plt.savefig('histogram_powerlaw.svg')
plt.show()