Introduction to Plotting
matplotlib and seaborn packages. Matlotlib is probably the most popular python package for 2D graphics and has a nice tradeoff between ease of use and customizability. We will be working with the pyplot interface, which is an object-oriented plotting library based on plotting in Matlab. Many graphics libraries are built on top of matplotlib, and have tried to make plotting even easier. One library that is very nice to generate plots similar to how analyses are performed is seaborn. There are many great tutorials online. Here are a few that I found to be helpful from neurohackademy, Jake Vanderplas, and rougier.
Matplotlib Key Concepts
There are a few different types of concepts in thematplotlib.pyplot framework.
Figure: Essentially the canvas, which contains the plotsAxes: An individual plot within a figure. Each Axes has a title, an x-label, and a y-labelAxis: These contain the graph limits and tickmarksArtist: Everything that is visiable on the figure is an artist (e.g., Text objects, Line2D object, collection objects). Artists are typically tied to a specific Axes.
%matplotlib inline is an example of 'cell magic' and enables plotting within the notebook and not opening a separate window. In addition, you may want to try using %matplotlib notebook, which will allow more interactive plotting.
Let's get started by loading the modules we will use for this tutorial.
Lineplot
First, let's generate some numbers and create a basic lineplot. In this example, we plot:
Scatterplot
We can also plot associations between two different variables using a scatterplot. In this example, we will simulate correlated data withnp.random.multivariate_normal and create a scatterplot. Try playing with the covariance values to change the degree of association.
Histogram
We can also plot the distribution of a variable using a histogram. Let's see if the data we simulated above are actually normally distributed.- 0: [ 3. 6. 26. 73. 81. 114. 97. 57. 32. 11.]
- 1: [-3.30388067 -2.71524649 -2.12661231 -1.53797813 -0.94934396 -0.36070978 0.2279244 0.81655858 1.40519275 1.99382693 2.58246111]
- 2:

- 0: [[ 4. 39. 107. 183. 128. 33. 5. 1. 0. 0.] [ 0. 0. 0. 4. 29. 100. 166. 148. 47. 6.]]
- 1: [-3.40439299 -2.44866447 -1.49293596 -0.53720745 0.41852106 1.37424957 2.32997808 3.28570659 4.24143511 5.19716362 6.15289213]
- 2: list · 2 items
- 0:

- 1:

- 0:
Bar Plot
We can also plot the same data as a bar plot to emphasize the difference in the means of the distributions. To create a bar plot, we need to specify the bar names and the heights of the bars.
3D Plots
We can also plot in 3 dimensions withmplot3d. Here we will simulate 3 different variables with different correlations.
Customization
One of the nice things about matplotlib is that everything is customizable. Let's go back to our first scatterplot and show how we can customize it to make it easier to read. We can specify the type ofmarker, the color, the alpha transparency, and the size of the dots with s.
We can also label the axes with xlabel and ylabel, and add a title.
Finally, we can add text annotations, such as the strength of the correlation with annotate.
Layouts
The easiest way to make customized layouts that can include multiple panels of a plot are withsubplot.ax object with rows and columns as I find it more intuitive.
You can do even more advanced layouts with panels of different sizes using gridspec.
Let's make our simulation code into a function and use subplots to plot multiple panels.
We specify the number of rows and columns when we initialize the plot. We can also play with the size of the plot. Here we tell matplotlib that the x and y axes will be shared across the different panels. Finally, plt.tight_layout() helps keep everything formatted and organized nicely.
When modifying axes we need to use the set_{} command rather than just the command itself. For example, ax[0,0].set_xlabel('X') rather than plt.xlabel('X').
Saving Plots
Plots can be saved to disk using thesavefig command. There are lots of ways to customize the saved plot. I typically save rasterized versions as .png and vectorized versions as .pdf. Don't forget to specify a path where you want the file written to.
Seaborn
Seaborn is a plotting library built on Matplotlib that has many pre-configured plots that are often used for visualization. Other great tutorials about seaborn are here Most seaborn plots can be customized using the standard matplotlib commands, though be sure to look at the docstrings first as often there are already keywords within each type of plot to do what you want.Scatterplots
There are many variants of each type of plot that you might like to use. For example,scatterplot, regplot, jointplot.
Let's update our simulation code a little bit to make this easier.
regplot.
jointplot.
pairplot.
Factor plots
Factor plots allow you to visualize the distribution of parameters in different forms such as point, bar, or violin graphs. One important thing to note is that the data needs to be in the long format. Let's quickly simulate some data to plot. Here are some possible values for kind : {point, bar, count, box, violin, strip}
Heatmaps
Heatmap plots allow you to visualize matrices such as correlation matrices that show relationships across multiple variables. Let's create a dataset with different relationships between variables. We can quickly calculate the correlation between these variables and visualize it with a heatmap
Pandas
We introducedPandas in a previous tutorial. It can also call matplotlib to quickly generate plots.
We will use the same dataset used in that tutorial to generate some plots using pd.Dataframe.
salary_in_departm column showing standardized salary per department.
Scatterplot
Generating bar - errorbar plots in Pandas
Interactive Plots
Interactive data visualizations are an exciting development and are likely to continue to grow in popularity with the rapid developments in web frontend frameworks. Covering these libraries is beyond the scope of this tutorial, but I highly encourage you to check them out. Some of them are surprisingly easy to use and make exploring your data and sharing your results much more fun. It is possible to add some basic interactivity to the plotting libraries covered in this tutorial in jupyter notebooks with ipywidgets. You will see a few examples of this in other tutorials.Plotly
Plotly is an graphing library to make interactive plots.Bokeh
Bokeh is an interactive visualization libraryAltair
Altair is a declarative statistical visualization library for Python based on VegaExercises
The following exercises uses the dataset "salary_exercise.csv" adapted from material available here These are the salary data used in Weisberg's book, consisting of observations on six variables for 52 tenure-track professors in a small college. The variables are:- sx = Sex, coded 1 for female and 0 for male
- rk = Rank, coded
- 1 for assistant professor,
- 2 for associate professor, and
- 3 for full professor
- yr = Number of years in current rank
- dg = Highest degree, coded 1 if doctorate, 0 if masters
- yd = Number of years since highest degree was earned
- sl = Academic year salary, in dollars.
Graded version
The graded version of these exercises is the Plotting assignment at the end of this page, which opens in a drawer from the Assignment button in the header. The exercises below ask you to recreate a figure exactly; the graded version checks the numbers behind the plot and the structure of the figure, and leaves the styling to you.Exercise 1
Recreate the plot shown in figure. On the left is a correlation of all parameters of only the male professors. On the right is the same but only for female professors. The colormap code used isRdBu_r. Read the Docstrings on sns.heatmap or search the internet to figure out how to change the colormap, scale the colorbar, and create square line boundaries.
Place titles for each plot as shown, and your name as the main title.
Exercise 2
Recreate the following plot from the salary_exercise.csv dataset. Create a 1 x 2 subplot. On the left is a bar-errorbar of salary per gender. On the right is a scatterplot of salary on y-axis and years in rank on the x-axis. Set the axis limits as shown in the picture and modify their lables. Add axis label names. Add a legend for the scatterplot and place it at a bottom-right location. Add your name as the main title of the plot.
Assignment: Introduction to Plotting
- Setup
- Q1. The numbers behind the heatmap
- Q2. Two heatmaps side by side
- Q3. Mean salary by sex, with error bars
- Q4. Salary against years in rank
- Q5. What the bar chart hides
Open in molab
Opens in a drawer at the bottom of the page, so you can keep reading while you work. Autosaves in this browser.