Sie sind auf Seite 1von 21

Lecture 8

Basics of plotting in Python:


matplotlib
Pylab or Pyplot?
• Matplotlib.pyplot 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.

plt.show is not mandatory, but


could be useful in your projects
and shows good coding
practice.
plt.show()?
plt.show() 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 plt.plot 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 plt.errorbar 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.!

plt.savefig 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 plt.show() or plt.close()

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:


https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html
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”


http://tylervigen.com/spurious-correlations
Histograms
8. Polar Plots
Following our last lecture,
we can define a function and then use
it in plt.plot
Further plotting
• Multiple panels per plot
• 2D Histograms
• Heatmaps
• Annotations
• 3D Plots

Most of these are covered in Chapter 7 of the


textbook

Das könnte Ihnen auch gefallen