- How to create Plotly animations
- How to save Plotly animations
- How to create Mapcharts – Plotly & Mapbox
- How to create Matplotlib animations
- How to save Matplotlib animations
- Titles, Axes, Ticks & Legend
- Matplotlib Built-in Styles
- Animated Line Charts
- Multi-line Animation
- Stacked Area Charts
- Word Cloud Visualization
- Bar Chart Animations
- Colors with Python
- Creating Multiple Charts
- Creating Hexbin Charts
- Creating Pcolor Charts
Pcolor and Pcolormesh
Pcolor and Pcolormesh functions can be used to create mosaic like charts or grids that visualize data in a specific and aesthetic way.
If you have used Github, pcolor plots can look very similar to the progress grid there. In this tutorial we will demonstrate how to create and save pcolor charts.
1- Pcolor and Pcolormesh
There are 2 functions that belong to pyplot module of matplotlib that can generate very similar visuals.
Main distinction between Pcolor and Pcolormesh is that former is not suitable for large datasets while latter is (Pcolormesh). Otherwise these plotting functions will yield very similar results.
plt.pcolor(Z, edgecolors='k', linewidths=2, cmap="Spectral", ) plt.pcolormesh(Z, cmap="viridis")
2- Basic Pcolor Plot Example
You can create hexbin charts using pyplot.hexbin function like below:
import matplotlib.pyplot as plt import numpy as np Z = np.random.rand(10, 30) fig = plt.figure(figsize=(7,2)) plt.pcolor(Z, cmap="copper")
Colormaps are useful color objects in Python that can be applied to certain types of charts including pcolor. You can find out more about colormaps in the tutorial link below:
3- Pcolor Multi-Plot Grid Example
Multiple charts combinations can create powerful and efficient visuals that can be used in many types of projects including this blog page. You can check out this related tutorial if you’d like to learn more about multiple charts in Python:
Here is a Multiple Pcolor Chart Grid using pyplot.pcolor function in combination with pyplot.subplot() like below:
import matplotlib.pyplot as plt import numpy as np fig = plt.figure(figsize=(8,10)) Z = np.random.rand(10, 30) plt.subplot(4, 1, 1) plt.pcolor(Z, edgecolors='k', linewidths=2, cmap="inferno", ) plt.subplot(4, 1, 2) plt.pcolor(Z, edgecolors='k', linewidths=2, cmap="Blues", ) plt.subplot(4, 1, 3) plt.pcolor(Z, edgecolors='k', linewidths=2, cmap="cividis", ) plt.subplot(4, 1, 4) plt.pcolor(Z, edgecolors='k', linewidths=2, cmap="Spectral", )
Pcolor and pcolormesh has similar parameters that can be useful to further adjust the grid charts created with them.
Edgecolors: This parameter is used to define the color of lines that separate the grids. You can also opt out of the edge option by assigning linewidths parameter explained below to 0.
Linewidths and Cmap Parameters
Linewidths is simply used to define the width of the lines between pcolor grids.
cmap can be used to apply colormaps across the chart.
4- How to save pcolor plots
As with other Pyplot charts you can save pcolor plots using the code below. Just enter the full or relative path inside the savefig method like below:
In this Python Visualization Tutorial we learned how to create and save Python pcolor charts using pyplot’s pcolor function.
We learned the main parameters and settings of pcolor and demonstrated this knowledge through a couple of examples.