8 Python chart examples using Matplotlib
Tags: python, programming, matplotlib, datascience
Published: 2022-11-03 11:01:22

## 1. 3D Scatter chart

```python
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

ax.scatter(1, 1, 1)
ax.scatter(2, 2, 1)
ax.scatter(2, 3, 0)

plt.show()
```

Where `projection='3d'` is used to set 3d mode for this chart, `.scatter(` plots a point chart.

![](https://onelinerhub.com/python-matplotlib/3d-scatter-chart-example.png)

[Open original](https://onelinerhub.com/python-matplotlib/3d-scatter-chart-example) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/3d-scatter-chart-example.md).

## 2. Table

Well, this is not a chart, but tables are still used sometimes.

```python
import matplotlib.pyplot as plt

data = [[1, 2, 3, 4, 5],
        [10,20,30,40,50],
        [11,21,31,41,51]]

plt.table(data, loc='center', colLabels=['A','B','C','D','E'])

plt.show()
```

Here `data` is the data to use for table cells. Then we call `.table(` to plot a table and `colLabels` to define column titles.

![](https://onelinerhub.com/python-matplotlib/how-to-draw-a-table.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-draw-a-table) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-draw-a-table.md).

## 3. Boxplot chart

```python
import matplotlib.pyplot as plt

plt.boxplot([2,3,6,2,4,5,1,10])
 
plt.show()
```

We just use `.boxplot(` to plot [boxplot chart](https://medium.com/datadenys/how-to-read-boxplot-c714f821a188).

![](https://onelinerhub.com/python-matplotlib/how-to-draw-boxplot.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-draw-boxplot) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-draw-boxplot.md).

## 4. Line chart

```python
import matplotlib.pyplot as plt
plt.plot([1,2,10,6,15,3,4])
plt.show()
```

The `.plot(` is used to plot a line based on given data.

![](https://onelinerhub.com/python-matplotlib/how-to-draw-line-chart.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-draw-line-chart) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-draw-line-chart.md).

## 5. Bar chart

```python
import matplotlib.pyplot as plt
plt.bar(['UA', 'UK', 'USA'], [10, 11, 12])
plt.show()
```

The `.bar` method will plot bar chart, while `['UA', 'UK', 'USA']` is used as x-axis values and `[10, 11, 12]` as y-axis values.

![](https://onelinerhub.com/python-matplotlib/how-to-make-bar-chart.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-make-bar-chart) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-make-bar-chart.md).

## 6. Heatmap

```python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
im = ax.imshow([[0.8, 2.4, 2.5], [1.3, 1.2, 0.0], [0.1, 2.0, 0.0]])

plt.show()
```

To draw heatmap we use `.imshow(`, which displays given data as an image.

![](https://onelinerhub.com/python-matplotlib/how-to-plot-heatmap.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-plot-heatmap) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-plot-heatmap.md).

## 7. Histogram

```python
import matplotlib.pyplot as plt

x = [1,2,5,1,2,3,5,6,7,4,2,2,4,5,6]
plt.hist(x,bins=5)
plt.show()
```

To build a histogram we use `.hist(` and set `bins` - number of histogram bins (bars) to group data into.

![](https://onelinerhub.com/python-matplotlib/how-to-plot-histogram.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-plot-histogram) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-plot-histogram.md).

## 8. World map

```python
import matplotlib.pyplot as plt
import geopandas

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.plot()
plt.show()
```

First, we load `geopandas` - module to work with maps and geo charts. Then world.plot()` can be used to plot world map.

![](https://onelinerhub.com/python-matplotlib/how-to-plot-world-map.png)

[Open original](https://onelinerhub.com/python-matplotlib/how-to-plot-world-map) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/python-matplotlib/how-to-plot-world-map.md).