Skip to content

Plotting

Marinna Martini edited this page Apr 27, 2020 · 9 revisions

Here is my current favorite matplotlib plot setup, a code fragment reminder for now:

lines = []
fig, axes = plt.subplots(figsize=(15,2))
lines.append(axes.plot(t[0],ds[0]['wh_4061'][:,0,0],color='g',label=lbls[0]))
lines.append(axes.plot(t[1],ds[1]['wh_4061'][:,0,0],color='b',linestyle=':',label=lbls[1]))
lines.append(axes.plot(t[2],ds[2]['wh_4061'][:],color='r',label=lbls[2]))

#axes.set_ylim(10,14)
#axes.set_xlim(pydt[0],pydt[-1])
#axes.set_xlim(pydt[0],pydt[-1])
#axes.set_title('T_28')
axes.legend(loc='best')

the setup might also be

fig = plt.figure(figsize=(15,5))
ax = fig.add_subplot()

Another variant is

fig, ax = plt.subplots(figsize=(15,2))
ax.plot(ds['time'][burst_number,:],ds[var][burst_number,:])
ax.set(xlabel='sec')
ax.set(ylabel=ds[var].units)
ax.set(title=f'burst {burst_number} on {cft[burst_number]} in {infileroot[1:]}')

A pcolor variant is this, a sonar image from a processed sonar file

img = ds['sonar_image'][19,0,:,:]
x = ds['x'][:]
y = ds['y'][:]

fig, ax = plt.subplots(1,1,figsize=(5,5))

c = ax.pcolormesh(x,y,img, cmap='gray')
ax.set_title(f'Imagenex 881a HF Sonar Image {t[idx[0]]:%Y-%m-%d}')
ax.set_xlabel('meters')
ax.set_ylabel('meters')
ax.axis('equal')

plt.show()

Saving these files is done by:

import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
import matplotlib.pyplot as plt
# make your plot here
fig.savefig('fig11_hffanimage.eps', format='eps')

And note that saving to pdf has given me better results.

Making your line plots line up with meshplots that have colorbars

Here as a gist: https://gist.github.com/11333a3bf0578a2599748917e2949d06

# This is to control the width of the plots so the time axis lines up
fig = plt.figure(constrained_layout=True,figsize=(15,10))
gs = fig.add_gridspec(2,6)

ax0 = fig.add_subplot(gs[0,:-1])
ax0.plot(np.sin(range(100)))

ax1 = fig.add_subplot(gs[1,:-1])
c1 = ax1.pcolormesh(np.random.random((20, 20)))
fig.colorbar(c1, ax=ax1)
Clone this wiki locally