Basic plotting
Last updated
Last updated
The required packages for this section are pandas, matplotlib and seaborn. These can be installed with the following command in the command window (Windows) / terminal (Mac).
The most straightforward way to load and manipulate tabular data in python is through the Pandas library. Pandas can load data from a range of different formats, the most commonly used are Excel or .csv files. In this tutorial we will use the lipidomics demo dataset (in excel format), which you can download with the link below.
Place the downloaded Lipidomics_dataset.xlsx file in the same folder as your JupyterLab script. Then run the following code in Jupyter:
The first line imports the pandas library and gives you access to this library through the alias "pd". On the second line we use the read_excel function of the pandas library, to which we pass between quotes the name of the file we want to load, or the full path to the file if it is not stored in the same folder as the Jupyter script. The loaded data is stored as a DataFrame in the df variable.
On the third line we call the head() function on the Dataframe object that holds our data, which will display the first 5 rows of the table. It should look like this:
The rows of the table correspond the samples, the columns to lipid species, except for the first two columns which contain the unique sample IDs and the sample labels (to which group samples belong). A handy improvement that allows us to easily access the data of individual samples by their unique ID, is to specify that the column "Sample Name" should be used as the index column:
Now if we want to access the data or for example sample 1a2, this can be done with:
The complete code for loading the data:
The pandas package has a number of built-in functions for the plotting of basic graphs. Under the hood pandas is relying on the matplotlib package for plotting, and to have more control over how to plot the data, we will also work with matplotlib directly. Now that the data is loaded and ready, let's load matplotlib:
Next, we define which lipid we want to plot, we group our DataFrame by the Label variable (which defines to which groups the samples belong) and we calculate the mean and standard deviation. If the standard error is desired instead, std() can be replaced with sem() instead.
Next, we load the fig and ax objects from matplotlib, which allows us to customize the titles (among many other parameters such as colors, line thickness, etc..., for which we refer to the matplotlib documentation. Finally, we use the plot.bar() function of the mean DataFrame, and pass in as arguments the errors, the customized axis with its titles (ax), and we define the size of the error whiskers (capsize=4), rotation of the labels (rot=0) and finally we pass in a list of color names.
We should get something that looks like this:
The complete code to make a barplot from a loaded pandas DataFrame:
For boxplots, the built-in plotting capabilities of the pandas package are quite powerful. We can simply call the plot.box() function on our DataFrame. In this function we pass a list of the lipid IDs that we want to have plotted, and the y-axis title:
The results should look like this:
By passing "Label" to the "by" argument, we can plot the species separately for the different Labels of the samples:
And to get more control over the appearance of the box plot, we can switch to seaborn and define our preferred colors:
Creating a histogram with Pandas is straightforward as well, we'll just need to load the ax object from matplotlib again to customize the axis titles:
And to show multiple groups with custom colors we can use seaborn:
Creating density plots is highly similar to creating histograms:
Or with seaborn:
Plots made with Pandas, Seaborn or Matplotlib can all be saved by running the following command after the creation of the plot:
This requires that pyplot from matplotlib is imported:
Instead of .png, vector formats such as .svg or .pdf can also be used, such that the figures can be edited in programs like Inkscape or Illustrator.