{ "cells": [ { "cell_type": "markdown", "id": "edf7b19c-20f1-43b4-b3cb-cdf6a2843749", "metadata": {}, "source": [ "# Plotting Data with Python" ] }, { "cell_type": "markdown", "id": "d387cfd5-3d79-4817-84b0-50da2bf09612", "metadata": {}, "source": [ "Data, be it images or object features, can and must be plotted for a better understanding of their properties or relationships. We already saw that we can use [napari]() to interactively visualize images. Sometimes, we may want to have a static view inside a notebook to consistently share with collaborators or as material in a publication.\n", "\n", "Python has many libraries for plotting data, like [matplotlib](https://matplotlib.org/stable/gallery/index.html), [seaborn](https://seaborn.pydata.org/), [plotly](https://plotly.com/python/) and [bokeh](https://docs.bokeh.org/en/latest/docs/gallery.html#standalone-examples), to name a few. Some libraries ship plotting function inside them as a convenience. For example, the pandas method [`.plot`](https://pandas.pydata.org/docs/user_guide/10min.html#plotting) can plot graphs directly from dataframes.\n", "\n", "In this notebook, we will explain the basics of [Matplotlib](https://matplotlib.org/stable/gallery/index.html), probably the most flexible and traditional library to display images and data in Python.\n", "\n", "Knowing a bit of its syntax help understanding other higher level libraries." ] }, { "cell_type": "code", "execution_count": 1, "id": "c9f956ed-5f7d-4448-80bb-aaabd605176b", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from skimage.io import imread\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "590bf61d-b47e-4f6c-b6ff-3694d3aa2df2", "metadata": {}, "source": [ "## Reading data" ] }, { "cell_type": "markdown", "id": "ea04652c-6049-4d30-980d-2118948967da", "metadata": {}, "source": [ "In this notebook, we will use an image and a table to plot. Let's read them.\n", "\n", "The table contains continuous data from 2 images, identified by the last categorical column 'file_name'." ] }, { "cell_type": "code", "execution_count": 3, "id": "838cfa27-36c7-4117-b03c-c7227d40b02b", "metadata": {}, "outputs": [], "source": [ "image1 = imread(\"../../data/BBBC007_batch/20P1_POS0010_D_1UL.tif\")\n", "\n", "df = pd.read_csv(\"../../data/BBBC007_analysis.csv\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "747ad7d7-dcb7-4790-a1bf-7566ca15a5a8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | area | \n", "intensity_mean | \n", "major_axis_length | \n", "minor_axis_length | \n", "aspect_ratio | \n", "file_name | \n", "
---|---|---|---|---|---|---|
0 | \n", "139 | \n", "96.546763 | \n", "17.504104 | \n", "10.292770 | \n", "1.700621 | \n", "20P1_POS0010_D_1UL | \n", "
1 | \n", "360 | \n", "86.613889 | \n", "35.746808 | \n", "14.983124 | \n", "2.385805 | \n", "20P1_POS0010_D_1UL | \n", "
2 | \n", "43 | \n", "91.488372 | \n", "12.967884 | \n", "4.351573 | \n", "2.980045 | \n", "20P1_POS0010_D_1UL | \n", "
3 | \n", "140 | \n", "73.742857 | \n", "18.940508 | \n", "10.314404 | \n", "1.836316 | \n", "20P1_POS0010_D_1UL | \n", "
4 | \n", "144 | \n", "89.375000 | \n", "13.639308 | \n", "13.458532 | \n", "1.013432 | \n", "20P1_POS0010_D_1UL | \n", "