Basic Plotting with Matplotlib (Scatterplot, Barplot, Heatmap, Boxplot, etc.) Cartopy and Map plotting Plotting Contourplot using Precipitation data Choosing colorbars Wind direction (u and v) Making a Skew-T with wind barbs using MetPy Color maps in Matplotlib and creating customized color bar Interactive plotting
Suggestions 23 june
Matplotlib is a Python library for creating high-quality visualizations and plots of data.It is the most well-known of all the python visualization packages and provides a variety of capabilities such as:
Matplotlib can gernerate high-quality graphics in a number of file types including PDF, SVF, JPG, PNG, BMP and GIF.
Matplotlib include line plots, scatter plots, histograms, bar charts, pie charts, and box plots.
Additionally, it enables 3D charting, and many additional libraries, such as pandas and Seaborn, have been built on top of it to provide access to Matplotlib's capabilities with less code.
Let's have a look at some of the basic function that are often used in matplotlib.
Function | Description |
---|---|
plt.plot() | Creates line plots to represent data. |
plt.scatter() | Generates scatter plots to display individual data points. |
plt.bar() | Creates bar plots to visualize categorical data. |
plt.hist() | Generates histograms to display the distribution of numerical data. |
plt.boxplot() | Creates boxplots to show the distribution and outliers of a dataset. |
plt.pie() | Generates pie charts to display proportions or percentages of categorical data. |
plt.imshow() | Displays images or heatmaps using arrays of data. |
plt.show() | it displays the created plots |
plt.xlabel() | This function is used to add a label to the x-axis of the plot. |
plt.ylabel() | This function is used to add a label to the y-axis of the plot. |
plt.title() | This function is used to add a title to the plot. |
plt.legend() | Adds a legend to the plot to identify plotted elements. |
plt.xticks() | This function is used to set the tick locations and labels for the x-axis. |
plt.yticks() | This function is used to set the tick locations and labels for the y-axis. |
plt.grid() | This function is used to add a grid to the plot. |
plt.xlim() | Sets the limits for the x-axis. |
plt.ylim() | Sets the limits for the y-axis. |
plt.annotate() | it is use to write comments on the graph at the specified position |
plt.figure(figsize = (x, y)) | whenever we want the result to be displayed in a separate window we use this command, and figsize argument decides what will be the initial size of the window that will be displayed after the run |
plt.subplot(r, c, i) | it is used to create multiple plots in the same figure with r signifies the no of rows in the figure, c signifies no of columns in a figure and i specifies the positioning of the particular plot |
These are some common functions used in matplotlib plotting.
To use matplotlib we need to import it.We will use standard shorthand for matplotlib imports to make things simpler.
import matplotlib.pyplot as plt
Here we have imported the pyplot
module from the matplotlib
library as plt
.This is a common convention to simplify the usage of pyplot
functions throughout the code.
Basically, we'll be using **`plt`** instead of **`matplotlib.pyplot`**
Step by step guide to create a scatterplot:
**Step 1:**
Firstly, we'll import the necessary packages.We'll use matplotlib
for plotting and numpy
to produce data.
**Step 2:**
Then we'll gernerate random data using numpy to plot.Here,we'll use Numpy's random.normal()
function to generate 100 random numbers for two variables:x
and y
**Step 3:**
In this step we'll plot the data using matplotlib's scatter()
function to create a scatter plot of the x
and y
data points.
Syntax of scatterplot :
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
**Step 4:** Finally, we use the show()
function to display the plot.
# import packages
import matplotlib.pyplot as plt
import numpy as np
# generate random data
x = np.random.normal(size=100)
y = np.random.normal(size=100)
# plot the data
plt.scatter(x, y)
# show the plot
plt.show()
Step by step guide to create a scatterplot:
**Step 1:** Firstly, we'll import the necessary packages.We'll use matplotlib
for plotting and numpy
to produce data.
**Step 2:**Then we'll gernerate random data using numpy to plot.Here,we'll use Numpy's NumPy's random.randint()
function to generate a list of 8 random integers between 1 and 150.
**Step 3:** In this step we'll use the bar()
function from pyplot to create a bar graph. The range(len(data))
is used to generate a sequence of indices corresponding to each data point, and data
represents the height of each bar. The color
parameter is set to '#900C3F' to specify the color of the bars.
Syntax of bar plot:
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
**Step 4:**Finally, we use the show()
function to display the plot.
#Importing packages
import matplotlib.pyplot as plt
import numpy as np
#Creating random list of 8 numbers.
data = list(np.random.randint(1, 150, 8))
#Plotting the horizontal bar.
plt.bar(range(len(data)), data, color='#900C3F')
#showing the plot
plt.show()
#Importing packages
import matplotlib.pyplot as plt
import numpy as np
#Creating random list of 8 numbers.
data = list(np.random.randint(1, 150, 8))
#Plotting the horizontal bar.
plt.barh(range(len(data)), data, color='#900C3F')
#showing the plot
plt.show()
A heatmap is a graphical representation of data where values are represented as colors in a two-dimensional grid. Heatmaps are particularly useful for visualizing patterns, correlations, or distributions in data matrices.
Let's see a heatmap
**Explanation:**
Here we see a heatmap representing the harvest data of local farmers. Each cell in the heatmap corresponds to a combination of a vegetable and a farmer, with the color intensity representing the harvest amount. The color intensity of each cell gives us the values of harvest with help of color bar included.
Step by step guide to create heatmap:
**Step 1:** Firstly, we'll import the necessary packages.We'll use matplotlib
for plotting and numpy
to produce data.
**Step 2:**Then we'll gernerate random data using numpy to plot.Here,we'll use Numpy's NumPy's np.random.rand()
function to generate a 10x10 random matrix.
**Step 3:** In this step we'll use the imshow()
function from pyplot to create a heatmap.The data
matrix is used as the input data. The cmap
parameter is set to autumn
to use the autumn color map.
Syntax of Heat map:
matplotlib.pyplot.imshow(X, cmap=None, alpha=None)
X :- this is input data matrix which is to be displayed
cmap :- Colormap we use t0 dispay the heatmap
alpha :- it specifies the opacity or transpiracy of the heatmap
**Step 4:** a color bar is then added.
**Step 5:**we set the title and labels.
**Step 6:**Finally, we use the show()
function to display the plot.
import matplotlib.pyplot as plt
import numpy as np
# Generate random data
data = np.random.rand(10, 10) # Generate a 10x10 random matrix
# Create a heatmap
plt.imshow(data, cmap='autumn')
# Add a color bar
plt.colorbar()
# Set title and labels
plt.title('Heatmap Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Show the plot
plt.show()
A boxplot also known as a box and whisker plot is a graphical representation of the distribution of a dataset.
It has the following component:
**Box:** The box represents the interquartile range (IQR), which contains the middle 50% of the data. The bottom edge of the box represents the 25th percentile (lower quartile), while the top edge represents the 75th percentile (upper quartile). The line inside the box represents the Median.
**Whiskers:** The whiskers extend from the box to the minimum and maximum non-outlier data points. They give an idea of the range of the dataset, excluding any outliers.
**Outliers:** Individual data points that are considered outliers are plotted as individual points outside the whiskers. They are typically defined as points that fall outside a certain multiple of the IQR from the edges of the box.By default, the range is set to 1.5 times the IQR.
Step by step guide to create a boxplot:
**Step 1:** Firstly, we'll import the necessary packages.We'll use matplotlib
for plotting and numpy
to produce data.
**Step 2:**Then we'll gernerate random dataset using numpy to plot.Here,we'll use Numpy's NumPy's random.normal()
function where the loc
parameter specifies the mean of the distribution, which is set to 0 in this case. The scale
parameter specifies the standard deviation of the distribution, which is set to 1. The size
parameter determines the number of data points in the dataset, which is set to 100.Then we added some outliers to data and created the final data.
**Step 3:** In this step we'll use the boxplot()
function from pyplot. The data
array is passed as the argument, which contains the dataset to be visualized.
Syntax of boxplot:
matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_ticks=True, autorange=False, zorder=None, capwidths=None, *, data=None)
**Step 4:**We add title and labels.
**Step 5:**Finally, we use the show()
function to display the plot.
import matplotlib.pyplot as plt
import numpy as np
# Generating random data
data = np.random.normal(loc=0, scale=1, size=100)
# Adding outliers
outliers = np.array([-3, 3]) # Example outliers
# Final data
data_with_outliers = np.concatenate((data, outliers))
# Create a boxplot
plt.boxplot(data_with_outliers)
# Set title and labels
plt.title('Boxplot Example')
plt.xlabel('Data')
# Show the plot
plt.show()
Cartopy is a Python package designed for geospatial data processing in order to make it easier to work with and analyze data that is related to the Earth's surface. It is built on top of several other popular scientific Python packages such as NumPy, Matplotlib, and Shapely, and provides a way to create maps, plot data on maps, and perform various geographical data analysis tasks.
Please note that when using cartopy in colab, it may sometimes crash. To avoid errors, please run the following cell. You can ignore the contents of the cell, and simply copy and paste the code whenever you need to use cartopy. After successfully installing all necessary components, please restart the runtime.
Runtime --> Restart Runtime --> do not run again after restart
# Run this cell when you are in colab and then restart runtime
# Do not worry about this cell, you can always copy paste
!apt-get install libproj-dev proj-data proj-bin
!apt-get install libgeos-dev
!pip install cython
!pip install cartopy
!apt-get -qq install python-cartopy python3-cartopy
!pip uninstall -y shapely # cartopy and shapely aren't friends (early 2020)
!pip install shapely --no-binary shapely
# importing libraries
import warnings
warnings. filterwarnings("ignore")
import matplotlib.pyplot as plt
# importing cartopy
import cartopy.feature as cf
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cticker
from cartopy.util import add_cyclic_point
Let's start by creating a basic map using cartopy. To do this, we first need to define an axes
and a projection
. Once the projection is defined, we can add coastlines to the map, specifying the linewidth and color. We'll delve deeper into projections
later on.
map = plt.axes(projection=ccrs.PlateCarree()) # defining axes and projection* and assigned it to `map`
map.coastlines() # add coastlines
We add features by calling the add_feature()
method on-axis object and passing a feature object available from the cartopy.feature
module.
We can add the following features (part of cartopy.feature
) to our map:
COASTLINE
: Adds coastline around all continents.LAND
: Adds land on top of world map.LAKES
: Adds big lakes of the world.BORDERS
: Adds country borders for the whole world.OCEAN
: Adds ocean with a color.RIVERS
: Adds big rivers of the world.# Adding more features and add stock images
plt.figure(figsize=(12,12))
map4 = plt.axes(projection=ccrs.PlateCarree())
map4.add_feature(cf.LAND)
map4.add_feature(cf.OCEAN)
map4.add_feature(cf.COASTLINE)
map4.add_feature(cf.BORDERS, linestyle=':')
map4.add_feature(cf.LAKES, alpha=0.5)
map4.add_feature(cf.RIVERS)
map4.stock_img() # try comment this