{ "metadata": { }, "nbformat": 4, "nbformat_minor": 5, "cells": [ { "id": "metadata", "cell_type": "markdown", "source": "
\n\n# Plotting in Python\n\nby [Maria Christina Maniou](https://training.galaxyproject.org/hall-of-fame/mcmaniou/), [Fotis E. Psomopoulos](https://training.galaxyproject.org/hall-of-fame/fpsom/), [The Carpentries](https://training.galaxyproject.org/hall-of-fame/carpentries/)\n\nCC-BY licensed content from the [Galaxy Training Network](https://training.galaxyproject.org/)\n\n**Objectives**\n\n- How can I create plots using Python in Galaxy?\n\n**Objectives**\n\n- Use the scientific library matplolib to explore tabular datasets\n\n**Time Estimation: 1H**\n
\n", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-0", "source": "

In this lesson, we will be using Python 3 with some of its most popular scientific libraries. This tutorial assumes that the reader is familiar with the fundamentals of data analysis using the Python programming language, as well as, how to run Python programs using Galaxy. Otherwise, it is advised to follow the “Introduction to Python” and “Advanced Python” tutorials available in the same platform. We will be using JupyterNotebook, a Python interpreter that comes with everything we need for the lesson.

\n
\n
Comment
\n

This tutorial is significantly based on the Carpentries Programming with Python and Plotting and Programming in Python, which is licensed CC-BY 4.0.

\n

Adaptations have been made to make this work better in a GTN/Galaxy environment.

\n
\n
\n
Agenda
\n

In this tutorial, we will cover:

\n
    \n
  1. Plot data using matplotlib
  2. \n
\n
\n

Plot data using matplotlib

\n

For the purposes of this tutorial, we will use a file with the annotated differentially expressed genes that was produced in the Reference-based RNA-Seq data analysis tutorial.

\n

Firstly, we read the file with the data.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-1", "source": [ "data = pd.read_csv(\"https://zenodo.org/record/3477564/files/annotatedDEgenes.tabular\", sep = \"\\t\", index_col = 'GeneID')\n", "print(data)" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-2", "source": "

We can now use the DataFrame.info() method to find out more about a dataframe.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-3", "source": [ "data.info()" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-4", "source": "

We learn that this is a DataFrame. It consists of 130 rows and 12 columns. None of the columns contains any missing values. 6 columns contain 64-bit floating point float64 values, 2 contain 64-bit integer int64 values and 4 contain character object values. It uses 13.2KB of memory.

\n

We now have a basic understanding of the dataset and we can move on to creating a few plots and further explore the data. matplotlib is the most widely used scientific plotting library in Python, especially the matplotlib.pyplot module.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-5", "source": [ "import matplotlib.pyplot as plt" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-6", "source": "

Simple plots are then (fairly) simple to create. You can use the plot() method and simply specify the data to be displayed in the x and y axis, by passing the data as the first and second argument. In the following example, we select a subset of the dataset and plot the P-value of each gene, using a lineplot.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-7", "source": [ "subset = data.iloc[121:, :]\n", "\n", "x = subset['P-value']\n", "y = subset['Gene name']\n", "\n", "plt.plot(x, y)\n", "plt.xlabel('P-value')\n", "plt.ylabel('Gene name')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-8", "source": "

\"A

\n

We use Jupyter Notebook and so running the cell generates the figure directly below the code. The figure is also included in the Notebook document for future viewing. However, other Python environments like an interactive Python session started from a terminal or a Python script executed via the command line require an additional command to display the figure.

\n

Instruct matplotlib to show a figure:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-9", "source": [ "plt.show()" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-10", "source": "

This command can also be used within a Notebook - for instance, to display multiple figures if several are created by a single cell.

\n

If you want to save and download the image to your local machine, you can use the plt.savefig() command with the name of the file (png, pdf etc) as the argument. The file is saved in the Jupyter Notebook session and then you can download it. For example:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-11", "source": [ "plt.tight_layout()\n", "plt.savefig('foo.png')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-12", "source": "

plt.tight_layout() is used to make sure that no part of the image is cut off during saving.

\n

When using dataframes, data is often generated and plotted to screen in one line, and plt.savefig() seems not to be a possible approach. One possibility to save the figure to file is then to save a reference to the current figure in a local variable (with plt.gcf()) and then call the savefig class method from that variable. For example, the previous plot:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-13", "source": [ "subset = data.iloc[121:, :]\n", "\n", "x = subset['P-value']\n", "y = subset['Gene name']\n", "\n", "fig = plt.gcf()\n", "plt.plot(x, y)\n", "fig.savefig('my_figure.png')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-14", "source": "

More about plots

\n

You can use the plot() method directly on a dataframe. You can plot multiple lines in the same plot. Just specify more columns in the x or y axis argument. For example:

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-15", "source": [ "new_subset = data.iloc[0:10, :]\n", "new_subset.loc[:, ['P-value', 'P-adj']].plot()\n", "plt.xticks(range(0,len(new_subset.index)), new_subset['Gene name'], rotation=60)\n", "plt.xlabel('Gene name')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-16", "source": "

\"The

\n

In this example, we select a new subset of the dataset, but plot only the two columns P-value and P-adj. Then we use the plt.xticks() method to change the text and the rotation of the x axis.

\n

Another useful plot type is the barplot. In the following example we plot the number of genes that belong to the different chromosomes of the dataset.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-17", "source": [ "bar_data = data.groupby('Chromosome').size()\n", "bar_data.plot(kind='bar')\n", "plt.xticks(rotation=60)\n", "plt.ylabel('N')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-18", "source": "

\"It

\n

matplotlib supports also different plot styles from ather popular plotting libraries such as ggplot and seaborn. For example, the previous plot in ggplot style.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-19", "source": [ "plt.style.use('ggplot')\n", "bar_data = data.groupby('Chromosome').size()\n", "bar_data.plot(kind='bar')\n", "plt.xticks(rotation=60)\n", "plt.ylabel('N')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-20", "source": "

\"The

\n

You can also change different parameters and customize the plot.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-21", "source": [ "plt.style.use('default')\n", "bar_data = data.groupby('Chromosome').size()\n", "bar_data.plot(kind='bar', color = 'red', edgecolor = 'black')\n", "plt.xticks(rotation=60)\n", "plt.ylabel('N')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-22", "source": "

\"The

\n

Another useful type of plot is a scatter plot. In the following example we plot the Base mean of a subset of genes.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "id": "cell-23", "source": [ "scatter_data = data[['Base mean', 'Gene name']].head(n = 15)\n", "\n", "plt.scatter(scatter_data['Gene name'], scatter_data['Base mean'])\n", "plt.xticks(rotation = 60)\n", "plt.ylabel('Base mean')\n", "plt.xlabel('Gene name')" ], "cell_type": "code", "execution_count": null, "outputs": [ ], "metadata": { "attributes": { "classes": [ "> " ], "id": "" } } }, { "id": "cell-24", "source": "

\"A

\n
\n
Question: Plotting
\n

Using the same dataset, create a scatterplot of the average P-value for every chromosome for the “+” and the “-“ strand.

\n
👁 View solution\n
\n

First find the data and save it in a new dataframe. Then create the scatterplot. You can even go one step further and assign different colors for the different strands.Note the use of the map method that assigns the different colors using a dictionary as an input.

\n
exercise_data = data.groupby(['Chromosome', 'Strand']).agg(mean_pvalue = ('P-value', 'mean')).reset_index()\n\ncolors = {'+':'red', '-':'blue'}\nplt.scatter(x = exercise_data['Chromosome'], y = exercise_data['mean_pvalue'], c = exercise_data['Strand'].map(colors))\nplt.ylabel('Average P-value')\nplt.xlabel('Chromosome')\n
\n

\"Another

\n
\n
\n

Making your plots accessible

\n

Whenever you are generating plots to go into a paper or a presentation, there are a few things you can do to make sure that everyone can understand your plots.

\n

Always make sure your text is large enough to read. Use the fontsize parameter in xlabel, ylabel, title, and legend, and tick_params with labelsize to increase the text size of the numbers on your axes.\nSimilarly, you should make your graph elements easy to see. Use s to increase the size of your scatterplot markers and linewidth to increase the sizes of your plot lines.\nUsing color (and nothing else) to distinguish between different plot elements will make your plots unreadable to anyone who is colorblind, or who happens to have a black-and-white office printer. For lines, the linestyle parameter lets you use different types of lines. For scatterplots, marker lets you change the shape of your points.

\n", "cell_type": "markdown", "metadata": { "editable": false, "collapsed": false } }, { "cell_type": "markdown", "id": "final-ending-cell", "metadata": { "editable": false, "collapsed": false }, "source": [ "# Key Points\n\n", "- Python has many libraries offering a variety of capabilities, which makes it popular for beginners, as well as, more experienced users\n", "- You can use scientific libraries like Matplotlib to perform exploratory data analysis.\n", "\n# Congratulations on successfully completing this tutorial!\n\n", "Please [fill out the feedback on the GTN website](https://training.galaxyproject.org/training-material/topics/data-science/tutorials/python-plotting/tutorial.html#feedback) and check there for further resources!\n" ] } ] }