Lecture 8
Basics of plotting in Python:
matplotlib
Pylab or Pyplot?
• [Link] is a much more powerful set of
plotting routines and can do everything pylab
can do.
• Pylab is more convenient for simple plotting.
• However we’re going to stick with pyplot to save
confusion.
Importing Pyplot and calling it
• When importing matplotlib there are 2
choices for the way our plots are shown:
– %matplotlib inline
– %matplotlib notebook
The difference between the two is that
notebook provides an interactive window inside
our Notebook
Our choices are:
or
But fair warning, changing from inline to
notebook in the same session might cause
glitches, lag or a complete inability to plot.
In that case, if you do wish to switch from inline
to notebook or vice versa:
Simply reload your Kernel.
1. Line Plots
- We generated our data points
(an array of some sorts for x and y)
- Then we call the plot function
- Finally we ask it to show what
has been plotted.
[Link] is not mandatory, but
could be useful in your projects
and shows good coding
practice.
[Link]()?
[Link]() not only shows us what we have
plotted to this point, but after being called it
resets the stage for new plots.
This is important if you plan to have multiple
plots in one cell of your code.
2. Scatter Plots
Unlike [Link] this function does not take into account the order of
our data and plots it as a marker.
3. Error Bars
- We can create mock errors for
each point by multiplying x and y
by an arbitrary value
(this case – 5%)
- Using [Link] we input the
values we have defined in a
specific order:
(x, y, y errors, x errors)
- The final value ‘o’ will be looked at
separately, but in short - it defines
the marker type we want.
In our case, it is a circle marker.
4. Saving plots to file
• Useful for embedding in presentations / reports (i.e.
projects!)
4. Saving plots to file
N.b.!
[Link] saves the figure without resetting the
stage. In that case one could accidently save
two plots, when they meant only one.
In that case, use [Link]() or [Link]()
It depends on if one wants to see the plot after
saving or just save it.
An example of this happening:
plot_1.png
plot_2.png
5. Customising our plots
Without setting markerfacecolor,
marker will be the same color as the
line.
If only the marker style is given, the
line will not be present and vice versa.
That means there are 3 choices:
line, line-points, and points
5. Customising our plots
Multiple plots on one figure, showing the diversity of options.
Full list of markers and line styles:
[Link]
6. Customising the figure
So far we have focused on customising the plot itself
(markers, lines etc.)
With regards to the figure itself there are multiple ways
to customise:
• Figure size
• Scale of axes (log, linear etc.)
• Title
• Axis Labels
• Limits to plot (allowing to zoom in/out)
• Font size (labels, legend, ticks on axes)
7. Twin Axis
• If we want to plot two things to compare, we will need two y-axes
Data from ”Spurious Correlations”
[Link]
Histograms
8. Polar Plots
Following our last lecture,
we can define a function and then use
it in [Link]
Further plotting
• Multiple panels per plot
• 2D Histograms
• Heatmaps
• Annotations
• 3D Plots
Most of these are covered in Chapter 7 of the
textbook