> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Pandas Visualization Cheat Sheet
- URL: https://datascientyst.com/pandas-visualization-cheat-sheet/
- Published: 2022-11-25T11:43:27.000Z
- Updated: 2022-11-25T11:43:27.000Z
- Author: John D K
- Tags: Cheat Sheet

**This visualization cheat sheet is a great resource to explore data visualizations with Python, Pandas and Matplotlib**. The Python ecosystem provides many packages for producing high-quality plots, graphs and visualizations.

In this guide, we will discuss the basics and a few popular visualization choices. The article starts with the basic steps for creating visualization. Next these steps are covered in detail. The end of this article has useful resources for visualizations - free books, guides, galleries.

There are summary images showing multiple visualizations at once. The goal of this guide is to help you building and customizing data visualizations.

Let's dive into visualization cheat sheet. Below you can find most popular plots from Seaborn:

![seaborn_barplot_lineplot_heatmap](https://datascientyst.com/content/images/2022/11/seaborn_barplot_lineplot_heatmap.webp)

## How to create good visualization

Python offers a ton of options and ways to visualize and summarize data which makes Python a natural choice for Data science.

Every great story starts with an idea. The same is with the visualization - we need idea and steps to follow to create great visualization.

1. Idea
2. Collect and select data
3. Data cleaning
4. Prepare data  
  1. dimensions
  2. X and Y axis data
  3. plot type - boxplot, line chart
5. Select tool
6. Select style and color palette
7. Customize the plot  
  1. title
  2. labels
  3. data format
  4. size

Let your data and plots tell your story.

## Data Setup

In this post we will use two DataFrames:

- DataFrame with random numbers
- Seaborn dataset

Creating DataFrame with 1000 numbers using normal distribution:

```python
import pandas as pd
import numpy as np

ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.head(5)

```

result:

|            | A          | B        | C          | D          |
| ---------- | ---------- | -------- | ---------- | ---------- |
| 2000-01-01 | \-0.004858 | 0.618783 | \-0.960541 | \-0.118617 |
| 2000-01-02 | \-0.476119 | 0.972206 | 0.457535   | \-0.099867 |
| 2000-01-03 | \-0.043310 | 0.218806 | \-0.751540 | \-0.501480 |
| 2000-01-04 | \-1.913368 | 0.143043 | 1.140921   | \-0.569990 |
| 2000-01-05 | 1.076793   | 0.809909 | 1.009482   | 0.716194   |

Seaborn DataFrame

```python
import seaborn as sns

glue = sns.load_dataset("glue").pivot("Model", "Task", "Score")
df_tit = sns.load_dataset("titanic")
penguins = sns.load_dataset("penguins")
df_sns = sns.load_dataset('flights')

```

data looks like is:

|   | year | month | passengers |
| - | ---- | ----- | ---------- |
| 0 | 1949 | Jan   | 112        |
| 1 | 1949 | Feb   | 118        |
| 2 | 1949 | Mar   | 132        |
| 3 | 1949 | Apr   | 129        |
| 4 | 1949 | May   | 121        |

## Pandas visualization cheat sheet

Pandas can visualize DataFrame by using the method `plot()`. It has a backend specified by the option `plotting.backend` \- by default - `matplotlib`.

Documentation for this method is available on this link: [DataFrame.plot](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html?ref=datascientyst.com).

### Setup, import, save

We need several imports to plot data with Python, Pandas and Matplotlib.

```python
import pandas as pd
import matplotlib.pyplot as plt

```

Save and show figure:

```python
plt.savefig('plot.png')
plt.savefig('plot.png',  transparent=True) #transparent

plt.show()

```

### Figure

To create new figure in Matplotlib with a given size:

- set size in inches
- figaspect will determine the width and height for a figure that would fit array preserving aspect ratio

```python
fig = plt.figure()

fig = plt.figure(figsize=(10,5)) # size in inches

fig = plt.figure(figsize=plt.figaspect(3.0))

w, h = figaspect(2.)
fig = Figure(figsize=(w,h))

```

### Axes

To add and delete axes

```python
fig.add_axes()
fig.add_axes(ax)
fig.delaxes(ax)

```

### Subplot

Working with Subplots in Matplotlib

```python
add_subplot(nrows, ncols, index, **kwargs)
add_subplot(pos, **kwargs)
add_subplot(ax)
add_subplot()

ax1 = fig.add_subplot(111) #row/col/ix
ax2 = fig.add_subplot(112)

fig, axes = plt.subplots(nrows=2,ncols=2)
fig, axes = plt.subplots(nrows=4)

```

### Matplotlib Markers

```python
ax.scatter(x,y,marker= ".")
ax.plot(x,y,marker= "o")

```

Available markers in Matplotlib:

- `"."` \- point
- `"o"` \- circle
- `"v"` \- triangle down
- `"s"` \- square
- `"D"` \- diamond
- `"*"` \- star marker  
To find more markers we can visit: [matplotlib.markers API](https://matplotlib.org/stable/api/markers%5Fapi.html?ref=datascientyst.com)

### Linestyles in Matplotlib

To find different line styles we can visit: [set\_linestyle](https://matplotlib.org/stable/api/%5Fas%5Fgen/matplotlib.lines.Line2D.html?ref=datascientyst.com#matplotlib.lines.Line2D.set%5Flinestyle):

- `'-'` \- solid line
- `'--'` \- dashed line
- `'-.'` \- dash-dotted line
- `':'` \- dotted line

```python
x = df['A']
y = df['B']

plt.plot(x,y,linewidth=5.0)
plt.plot(x,y,linestyle= 'solid' , color='y')
plt.plot(y,x,ls= '--')
plt.plot(y,x,'--' ,x**2,y**3,'-.' )

```

Plot different line styles with Matplotlib:

- color
- style

![linestyles-in-matplotlib](https://datascientyst.com/content/images/2022/11/linestyles-in-matplotlib.webp)

```python
from math import *
import numpy as np

x = np.arange(0,1.0,0.01)
y1 = np.sin(2*pi*x)
y2 = np.sin(4*pi*x)
lines = plt.plot(x, y1, x, y2)

plt.setp(lines, linewidth=2)

```

Plot different color lines with `plt.setp`:

![matplotlib-linestyles](https://datascientyst.com/content/images/2022/11/matplotlib-linestyles.webp)

```python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

cycler = plt.cycler(linestyle=['-', ':', '--', '-.'],
                    color=['r', 'b', 'y', 'g'])
fig, ax = plt.subplots()
ax.set_prop_cycle(cycler)
df.plot(ax=ax)
plt.show()

```

multiple line styles and colors:

![matplotlib-multiple-linestyles-colors](https://datascientyst.com/content/images/2022/11/matplotlib-multiple-linestyles-colors.webp)

### Pandas plot Series

To plot Pandas Series we can call method `plot()` directly on the Series:

```python
import pandas as pd
s = pd.Series([5, 7, 2, 4, 1])
ax = s.plot(kind='bar', figsize=(10,5))

```

The result is bar plot from the Series:

![pandas_plot_series](https://datascientyst.com/content/images/2022/11/pandas_plot_series.webp)

### Pandas plot DataFrame

We can Plot DataFrame in Pandas by calling the method `plot()`.

```python
ax = df.plot()

```

By default all numeric columns will be used for the visualization:

![pandas-plot-dataframe](https://datascientyst.com/content/images/2022/11/pandas-plot-dataframe.webp)

Prior plotting DataFrame we can:

- select only the columns that will be plot
- set index or select X and Y data
- format and clean data

To plot DataFrame as bar plot, using the year and month as X axis with custom figure size we can do:

```python
df.set_index(['year', 'month']).plot(kind='bar', figsize=(30,10))

```

This will plot number of passengers as Y axis:

![pandas_plot_multiple_columns_x_axis](https://datascientyst.com/content/images/2022/11/pandas_plot_multiple_columns_x_axis.webp)

### Title, Labels, Legend

```python
ax.set_xlabel('Year')
ax.set_ylabel('Passenger')
ax.set_title('Passengers per year')
ax.legend(labels, loc='best')

ax.set(title= 'Title', ylabel= 'Y label',  xlabel= 'X axis')

```

### Ticks

```python
import pandas as pd
s = pd.Series([5, 7, 2, 4, 1])
ax = s.plot(figsize=(10,5))

ax.yaxis.set(ticks=range(1,9,3), ticklabels=['min', 'mid', 'max'])
ax.tick_params(axis= 'y', direction= 'out', length=5)

```

result:

![pandas_plot_ticks](https://datascientyst.com/content/images/2022/11/pandas_plot_ticks.png)

### Margins, Limits

```python
import pandas as pd
s = pd.Series([5, 7, 2, 4, 1])
ax = s.plot(figsize=(10,5))

ax.margins(x=0.5,y=0.5)
# ax.axis('equal') # Equal axis size
# ax.set_xlim(1,5) # set x limit
ax.set(xlim=[-1,5],ylim=[1,9]) # set x & y limits

```

result:

![pandas_plot_margin_limit](https://datascientyst.com/content/images/2022/11/pandas_plot_margin_limit.webp)

### Parameters

- `x` \- X axis data
- `y` \- Y axis data
- `kind='bar'` \- plot type
- `ax` \-
- `figsize` \- plot size in inches
- `subplots=True` \- subplots for each column  
  - `sharex=False` \- in case of subplots - should X axis be shared
  - `layout=(3,2)` \- shape of the subplots - number of rows and columns
- `title` \- title of the plot
- `xticks/yticks` \- values to use for the xticks/yticks
- `xlabel` / `ylabel` \- name to use for the labels on x-axis/y-axis
- `fontsize=12` \- font size for title
- `color='green'` \- plot color  
  - `color = ['lightblue', 'r', 'y']`

more parameters on: [plot()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html?ref=datascientyst.com)

Example for parameter `subplots=True`:

![pandas_subplots_True](https://datascientyst.com/content/images/2022/11/pandas_subplots_True.webp)

### Display two plots - side by side

```python
from matplotlib import pyplot as plt

# First plot
ax = plt.subplot()
plt.pie( data=df, x='A')
plt.title( 'bar' )
plt.show()

# Second plot
ax = plt.subplot()
plt.scatter( data=df, x='A', y='B' )
plt.title( 'scatter' )
plt.show()

```

Two plots side by side - pie chart and scatter plot:

![display-two-plots-side-by-side](https://datascientyst.com/content/images/2022/11/display-two-plots-side-by-side.webp)

### Matplotlib subplots

```python
import matplotlib.pyplot as plt
import numpy as np

data = np.array([1, 4, 2, 3, 2])
plt.subplot(121)
plt.plot(data)

data = np.array([5, 7, 3, 8, 3])
plt.subplot(122)
plt.plot(data)
plt.show()

```

result:

![matplotlib_subplots](https://datascientyst.com/content/images/2022/11/matplotlib_subplots.webp)

### Grids of Subplots

```python
for i in range(1, 5):
    plt.subplot(2, 2, i)
    plt.text(0.5, 0.5, str((2, 2, i)),
             font size=18, ha='center')

```

result:

![matplotlib_grid_subplots](https://datascientyst.com/content/images/2022/11/matplotlib_grid_subplots.webp)

### Types of Pandas plots

List of the available chart types for `plot()` method. The plot type can be set as parameter - `kind`:

- `line` : line plot (default)
- `bar` : vertical bar plot
- `barh` : horizontal bar plot
- `hist` : histogram
- `box` : boxplot
- `kde` : Kernel Density Estimation plot
- `density` : same as 'kde'
- `area` : area plot
- `pie` : pie plot
- `scatter` : scatter plot (DataFrame only)
- `hexbin` : hexbin plot (DataFrame only)

Every available plot in Pandas is shown below:

![pandas-plot-dataframe_all_types](https://datascientyst.com/content/images/2022/11/pandas-plot-dataframe_all_types_bar_line_histogram.webp)

The code below generates all plots:

```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set_theme()

ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.head(5)

plots = [ 'line', 'hist', 'box', 'kde', 'density', 'area', 'pie', 'scatter', 'barh', 'bar', 'hexbin']

cols = df.columns
row_num = 3
col_num = 4
row_n = -1
col_n = 0

fig, axes = plt.subplots(row_num, col_num, squeeze=False, figsize=(20,14))

for ix, plot in enumerate(plots):
    axes[row_n, col_n].title.set_size(20)
    axes[row_n, col_n].title.set_color('red')
    col_n = ix % col_num
    if col_n == 0:
        row_n = row_n + 1
    if plot not in ['area', 'pie', 'scatter', 'hexbin']:
        df.plot(kind=plot,  ax=axes[row_n, col_n], title=plot, figsize=(30,12))
    elif plot == 'area':
        df.plot(kind=plot,  ax=axes[row_n, col_n], title=plot, stacked=False)
    elif plot == 'pie':
        series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"], name="series")
        series.plot.pie(ax=axes[row_n, col_n], title=plot);
    elif plot == 'scatter':
        df.plot(kind=plot,  ax=axes[row_n, col_n], title=plot, x=['A'], y=['B'])
    elif plot == 'hexbin':
        df.plot(kind=plot,  ax=axes[row_n, col_n], title=plot, x=['A'], y=['B'])

plt.show()

```

## Seaborn vs Matplotlib

Seaborn is based on Matplotlib. It enhances Matplotlib by simplifying the plot process and adding new features.

On the image below we can see all Seaborn plots like:

- [heatmap](https://seaborn.pydata.org/generated/seaborn.heatmap.html?ref=datascientyst.com)
- [boxplot](https://seaborn.pydata.org/generated/seaborn.boxplot.html?ref=datascientyst.com)
- [barplot](https://seaborn.pydata.org/generated/seaborn.barplot.html?ref=datascientyst.com)
- [lineplot](https://seaborn.pydata.org/generated/seaborn.lineplot.html?ref=datascientyst.com)
- [histogram](https://seaborn.pydata.org/generated/seaborn.histplot.html?ref=datascientyst.com)
- pie chart - currently Seaborn doesn't have pie chart

![seaborn_barplot_lineplot_heatmap](https://datascientyst.com/content/images/2022/11/seaborn_barplot_lineplot_heatmap.webp)

### Seaborn setup

We can import and load datasets with seaborn by next code:

```python
import seaborn as sns

glue = sns.load_dataset("glue").pivot("Model", "Task", "Score")
df_sns = sns.load_dataset('flights')
df_tit = sns.load_dataset("titanic")
penguins = sns.load_dataset("penguins")

```

### Seaborn heatmap

To plot heatmap with seaborn we can do simply:

```python
sns.heatmap(glue)

```

![seaborn_heatmap](https://datascientyst.com/content/images/2022/11/seaborn_heatmap.webp)

### Seaborn boxplot

Plotting boxplot in Seaborn is as easy as:

```python
df_tit = sns.load_dataset("titanic")
sns.boxplot(x=df["age"])

```

### Seaborn barplot

For barplot we need to:

- select data source
- X and Y axis - data

```python
penguins = sns.load_dataset("penguins")
sns.barplot(data=penguins, x="island", y="body_mass_g")

```

### Seaborn histogram

Seaborn histogram is called - `histplot`:

```python
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm")

```

### Seaborn multiple plots

To plot multiple visualization in Seaborn side by side we can do:

```python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('penguins')

sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.show()

sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.show()

```

result:

![seaborn_multiple_plots](https://datascientyst.com/content/images/2022/11/seaborn_multiple_plots.webp)

## Colors

Python comes with a huge variety of named colors and palettes like the one shown below. To find the full list of colors check:

- [Full List of Named Colors in Pandas and Python](https://datascientyst.com/full-list-named-colors-pandas-python-matplotlib/)
- [How to Get a List of N Different Colors and Names in Python/Pandas](https://datascientyst.com/get-list-of-n-different-colors-names-python-pandas/)

Changing colors in matplotlib:

```python
color = 'red'
mpl.rcParams['text.color'] = color
mpl.rcParams['axes.labelcolor'] = color
mpl.rcParams['xtick.color'] = 'y'
mpl.rcParams['ytick.color'] = 'b'

```

![matplotlib-colors](https://datascientyst.com/content/images/2022/11/matplotlib-colors.webp)

## Data Visualization Libraries

- [Matplotlib ](https://matplotlib.org/?ref=datascientyst.com) \- most popular and widely-used plotting library  
  - [Examples](https://matplotlib.org/stable/plot%5Ftypes/index.html?ref=datascientyst.com)
  - [Reference](https://matplotlib.org/stable/index.html?ref=datascientyst.com)
  - [Cheat Sheets](https://matplotlib.org/cheatsheets/?ref=datascientyst.com)
- [seaborn ](https://seaborn.pydata.org/?ref=datascientyst.com) \- based on Matplotlib. High-level interface for drawing attractive and informative statistical graphics  
  - [Gallery](https://seaborn.pydata.org/examples/index.html?ref=datascientyst.com)
  - [Tutorial](https://seaborn.pydata.org/tutorial.html?ref=datascientyst.com)
- [plotly](https://plotly.com/?ref=datascientyst.com) \- Plotly's Python graphing library makes interactive, publication-quality graphs.  
  - [Getting Started](https://plotly.com/python/getting-started?ref=datascientyst.com)
  - [Examples](https://plotly.com/python/?ref=datascientyst.com)
- [bokeh](https://bokeh.org/?ref=datascientyst.com) \- interactive visualization library  
  - [Gallery](https://docs.bokeh.org/en/latest/docs/gallery.html?ref=datascientyst.com)
  - [First steps](https://docs.bokeh.org/en/latest/docs/first%5Fsteps.html?ref=datascientyst.com)
- [Vega-Altair](https://altair-viz.github.io/?ref=datascientyst.com) \- produces beautiful and effective visualizations with a minimal amount of code.  
  - [Example Gallery](https://altair-viz.github.io/gallery/index.html?ref=datascientyst.com)
  - [User Guide](https://altair-viz.github.io/user%5Fguide/data.html?ref=datascientyst.com)
- [pygal](https://www.pygal.org/en/stable/?ref=datascientyst.com) is a dynamic SVG charting library written in python
- [geoplotlib](https://pypi.org/project/geoplotlib/?ref=datascientyst.com) \- open-source Python library for visualizing geographical data.

## Materials/Books for data visualization

### Free Materials

- [Fundamentals of Data Visualization](https://clauswilke.com/dataviz/?ref=datascientyst.com)

  - [github](https://github.com/clauswilke/dataviz?ref=datascientyst.com)
- [an introduction to VISUALIZING DATA](https://beforebefore.net/scima300/f15/media/visualizingdata.pdf?ref=datascientyst.com)
- [Getting Started with Data Visualization](https://web.stanford.edu/group/toolingup/cgi-bin/toolkit/wp-content/uploads/2011/03/GMcGhee%5Ftoolingup%5FDataVis%5F110506.pdf?ref=datascientyst.com)
- [Principles of Data Visualization I ](https://indico.cern.ch/event/681081/contributions/2790760/attachments/1729504/2794629/Principles-of-Visualization-Course-Pt1-Full.pdf?ref=datascientyst.com)
- [Visualization with Matplotlib - Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/04.00-introduction-to-matplotlib.html?ref=datascientyst.com)

### Paid Books

- [Storytelling with Data: A Data Visualization Guide for Business Professionals](https://www.storytellingwithdata.com/books?ref=datascientyst.com) \- Don't simply show your data—tell a story with it!
- [Storytelling with Data: Let's Practice!](https://www.storytellingwithdata.com/books?ref=datascientyst.com) \- Influence action through data!
- [Information is Beautiful ](https://informationisbeautiful.net/books/?ref=datascientyst.com) \- A stunning visual journey through the most amazing, beautiful, and positive things happening in the modern world.
- [Better Data Visualizations A Guide for Scholars, Researchers, and Wonks](http://cup.columbia.edu/book/better-data-visualizations/9780231193115?ref=datascientyst.com) \- essential strategies to create more effective data visualizations

### Collections

- [R Data Visualization Books](https://www.bigbookofr.com/data-visualization.html?ref=datascientyst.com) \- 17 books for Data Visualization and R

### Resources Data Visualization

- [https://www.python-graph-gallery.com/](https://www.python-graph-gallery.com/?ref=datascientyst.com) \- collection of hundreds of charts made with Python
- [https://www.data-to-viz.com/](https://www.data-to-viz.com/?ref=datascientyst.com) \- leads you to the most appropriate graph for your data
- [https://www.reddit.com/r/dataisbeautiful/](https://www.reddit.com/r/dataisbeautiful/?ref=datascientyst.com) \- DataIsBeautiful is a reddit for visualizations that effectively convey information
- [https://www.reddit.com/r/dataisugly/](https://www.reddit.com/r/dataisugly/?ref=datascientyst.com) \- bad data visualizations