A simple Chart with Python

To make a simple chart bar with Python you need just matplotlib (pip install matplotlib):

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 7, 1]
ind = range(len(a))
p = plt.bar(ind, a)
plt.show(p)

The bar method of pyplot needs and index (ind) for the number of single data into the dataset. I took the number with the built in function len applied to the dataset ‘a’. After the ind, we pass the dataset (a) to plt and then we show the graphic with the method show.

Using a function

In case you like to have clean code, easy to reuse, I made this code:

import matplotlib.pyplot as plt

def show_plot(x):
    "Take an array/list and shows it"
    p = plt.bar(range(len(x)), x)
    plt.show(p)

a = [1, 2, 3, 4, 7, 1]
show_plot(a)
from mpld3 import plugins

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.