{ "cells": [ { "cell_type": "markdown", "id": "a20e9da6-4709-442c-b8f6-8198c8be99d1", "metadata": {}, "source": [ "# Matplotlib tutorial (Advanced) \n", "\n", "This tutorial continues on plotting with Python. Two packages will be introduced: __Seaborn__ and __Cartopy__. Seaborn is a Python library for making statistical graphics based on Matplotlib and Cartopy is a Python package designed for geospatial data visualization. We will first illustrate alternative ways for plots in statistics with Seaborn, then show plotting of geospatial maps with Cartopy.\n", "\n", "* Plots in statistics with Seaborn: histograms, density plots, ecdf plots, bar charts, box plots, and scatter plots\n", "* Plotting maps with Cartopy: a simple example, adding features to the map, and plotting 2D data" ] }, { "cell_type": "markdown", "id": "4b3effb7-97fc-44a6-976a-7e0c024c750e", "metadata": {}, "source": [ "## Plots in statistics with Seaborn\n", "\n", "Seaborn helps you explore and understand your data. Its plotting functions operate on dataframes and arrays containing whole datasets and internally perform the necessary semantic mapping and statistical aggregation to produce informative plots. Its dataset-oriented, declarative API lets you focus on what the different elements of your plots mean, rather than on the details of how to draw them. Compared with Matplotlib, Seaborn helps you save your time and efforts on customizing the figures.\n", "\n", "We first import the packages that might be used." ] }, { "cell_type": "code", "execution_count": 1, "id": "d5bb6077-2102-4cf0-8ea9-4458ce25d58c", "metadata": {}, "outputs": [], "source": [ "import seaborn as sns\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "id": "55d67640-bce5-4cb5-af18-18f6b936c463", "metadata": {}, "source": [ "Here, we utilize the \"diamonds\" dataset from Seaborn package for the demonstration of plots in statistics." ] }, { "cell_type": "code", "execution_count": 14, "id": "7cbefe80-e4af-4f4b-a283-40d2e95c90a5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | carat | \n", "cut | \n", "color | \n", "clarity | \n", "depth | \n", "table | \n", "price | \n", "x | \n", "y | \n", "z | \n", "
---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0.23 | \n", "Ideal | \n", "E | \n", "SI2 | \n", "61.5 | \n", "55.0 | \n", "326 | \n", "3.95 | \n", "3.98 | \n", "2.43 | \n", "
1 | \n", "0.21 | \n", "Premium | \n", "E | \n", "SI1 | \n", "59.8 | \n", "61.0 | \n", "326 | \n", "3.89 | \n", "3.84 | \n", "2.31 | \n", "
2 | \n", "0.23 | \n", "Good | \n", "E | \n", "VS1 | \n", "56.9 | \n", "65.0 | \n", "327 | \n", "4.05 | \n", "4.07 | \n", "2.31 | \n", "
3 | \n", "0.29 | \n", "Premium | \n", "I | \n", "VS2 | \n", "62.4 | \n", "58.0 | \n", "334 | \n", "4.20 | \n", "4.23 | \n", "2.63 | \n", "
4 | \n", "0.31 | \n", "Good | \n", "J | \n", "SI2 | \n", "63.3 | \n", "58.0 | \n", "335 | \n", "4.34 | \n", "4.35 | \n", "2.75 | \n", "
<xarray.Dataset>\n", "Dimensions: (latitude: 551, longitude: 1041, time: 12)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2020-01-01 2020-02-01 ... 2020-12-01\n", " * longitude (longitude) float32 -18.0 -17.9 -17.8 -17.7 ... 85.8 85.9 86.0\n", " * latitude (latitude) float32 51.0 50.9 50.8 50.7 ... -3.7 -3.8 -3.9 -4.0\n", "Data variables:\n", " t2m (time, latitude, longitude) float32 ...\n", " tp (time, latitude, longitude) float32 ...\n", "Attributes:\n", " CDI: Climate Data Interface version 1.9.9rc1 (https://mpimet.mpg...\n", " Conventions: CF-1.6\n", " history: Mon Oct 11 09:09:15 2021: cdo selname,tp,t2m ERA5_monthly_2...\n", " CDO: Climate Data Operators version 1.9.9rc1 (https://mpimet.mpg...
array(['2020-01-01T00:00:00.000000000', '2020-02-01T00:00:00.000000000',\n", " '2020-03-01T00:00:00.000000000', '2020-04-01T00:00:00.000000000',\n", " '2020-05-01T00:00:00.000000000', '2020-06-01T00:00:00.000000000',\n", " '2020-07-01T00:00:00.000000000', '2020-08-01T00:00:00.000000000',\n", " '2020-09-01T00:00:00.000000000', '2020-10-01T00:00:00.000000000',\n", " '2020-11-01T00:00:00.000000000', '2020-12-01T00:00:00.000000000'],\n", " dtype='datetime64[ns]')
array([-18. , -17.9, -17.8, ..., 85.8, 85.9, 86. ], dtype=float32)
array([51. , 50.9, 50.8, ..., -3.8, -3.9, -4. ], dtype=float32)
[6883092 values with dtype=float32]
[6883092 values with dtype=float32]