Onelinerhub

Posted on

# 8 Python chart examples using Matplotlib

## 1. 3D Scatter chart

import matplotlib.pyplot as plt

fig = plt.figure()

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.

## 2. Table

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

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.

## 3. Boxplot chart

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.

## 4. Line chart

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.

## 5. Bar chart

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.

## 6. Heatmap

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.

## 7. Histogram

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.

## 8. World map

import matplotlib.pyplot as plt
import geopandas

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.

DEV Community

Timeless DEV post...

## Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.