Skip to content

Plotting

Marinna Martini edited this page May 4, 2020 · 9 revisions

Matplotlib API for axes: https://matplotlib.org/api/axes_api.html

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)

Bokeh, Holoviews and hvplot

I am slowly groking these. The confusing in my mind is what gets inherited from where, what is the calling syntax, and which is the best object to use (line, Curve, etc.) for what features and annotations you need. For instance, the basic comparison plot for several sites in a deployment. I wanted a two line title. Some great help on this is found at: https://stackoverflow.com/questions/61417484/how-can-i-achieve-a-multiline-title-for-a-holoviews-plot/61460917#61460917

import numpy as np
import holoviews as hv
import bokeh.io
import bokeh.models
from bokeh.models import Title
hv.extension('bokeh')

points = [(0.1*i, np.sin(0.1*i)) for i in range(100)]

hv_curve = hv.Curve(points)
bk_curve = hv.render(hv_curve)
bk_curve.add_layout(Title(text="Sub-Title", text_font_style="italic"), 'above')
bk_curve.add_layout(Title(text="Title", text_font_size="16pt", text_font_style="bold"), 'above')
bokeh.io.show(bk_curve)