Python Plotting¶
Choose to start with pandas or netcdf and read in a particular data set that seems fun. (take a look at what software carpentries does). This plotting tutorial will be about matplotlib (i.e., assume they are not using cartopy, geopandas, or xarray. That content is for Thursday).
Set Up¶
Load the packages we will need¶
Load in the Dataset¶
Visualizing Data with Matplotlib¶
`insert link to matplotlib documentation website <>`__
Go over the following basic examples, if you need to you can split this into a set of examples with one dataset, and then a set of examples with a second dataset. Certain data might provide better examples for scatter or pcolormesh.
After introducing the most common examples, I would like to have some exercises people can pursue, such as adding contours on top of an imshow() or pcolormesh() plot. Use adding std deviation shading for one of the basic examples since that is particularly useful. Then you could make an exercise to add error bars to the std dev shading, something like that.
There are two formats for plotting, one is to always use subfigure() which I think we should teach. This will make all plots (even single figure plots) the same format. So the plot would start with:
fig, ax = plt.subplots(figsize=(),nrows=,ncols=)
If it’s a single figure (nrows=1,ncols=1) then ax is size 1. if there are subfigures then ax is a vector/matrix size nrows x ncols. This means you will use ax.plot()
and ax.set_xlim()
style, instead of plt. I still think we should start with single figure and move to multiple subfigures. but the style should remain the same.
I would give one example (like plot() or imshow()) and then spend some time on adding axis labels, setting axis limits, turning on legends, changing legend formatting. Then move on to more example functions like contour, etc. That way you don’t have to show how to add labels and things for every example, people can do that on their own.
Basic types of plotting functions in matplotlib.pyplot library:
1) plt.plot() : line plots
2) plt.contour() : contour plots
3) plt.contourf() : contour filled plots (a discrete pcolor plot with countours)
4) plt.pcolor() or pcolormesh() : colormap with continous or discrete colorbars
5) plt.scatter() : scatter plot
6) plt.imshow() : image plot
7) plt.fill_between() and error() : line plots with shadded regions and error bars
Single Figure Plotting:
Plotting Subplots:
Formatting Figures¶
Teach things like plt.tight_layout()
and adding a text bubble to your figures (not a legend). I can give an example if you need.