How to Create a Chart in Python
Displaying data in the form of charts or graphs in Python requires the use of specialized, external code libraries, and the open-source community offers a programmer a number of good ones to choose from. This demonstration uses the matplotlib library to show how to make a simple chart in Python. Matplotlib has both a broad and deep feature set and is well documented; this makes it a good place to start. If time allows, interested programmers should research other available libraries before committing to any one of them.
Things You'll Need
- Python scripting environment
- Text or code editor
- Matplotlib 2D plotting library
Instructions
-
Creating a Chart Using Python
-
1
Import the matplotlib pyplot module into the code file:
import matplotlib.pyplot as plt
-
2
Define variables used for bounding the chart axes and creating data to be plotted:
max_x, max_y, min_x, min_y = 11.0, 11.0*11.0, 0.0, 0.0
-
-
3
Declare arrays to separately hold x and y values to be plotted:
x_arr = []
y_arr = [] -
4
Fill the arrays with data. Typically you will draw this data from external sources such as files or database queries. Here the code creates sample data, implementing the formula y(x) = x*x:
for i in range(min_x,max_x):
x_arr.append(float(i))
y_arr.append(float(i*i)) -
5
Create a FigureCanvas object using the imported matplotlib pyplot object:
fig = plt.figure()
-
6
Add the graph’s axes to the FigureCanvas by calling the function "add_axes" and passing it an array of values in the form of: left, bottom, width, height. These values define where the graph is placed on the canvas. Values can range from 0.0 to 1.0:
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
-
7
Format the graph, adding labels and defining the minimum and maximum values for each axis:
ax.set_xlabel('x data')
ax.set_ylabel('y data')
ax.set_xlim(min_x,max_x)
ax.set_ylim(min_y,max_y) -
8
Plot the graph by passing in the two arrays containing the x and y values retrieved from the CSV file. Customize the line plot by passing in optional values such as line color (color) or line width (lw):
ax.plot(x_arr,y_arr, color='red', lw=2)
-
9
Call the pyplot module’s show method to display the chart in a window. This functionality and the style of window will depend on the operating system environment and the local installation of matplotlib:
plt.show()
-
10
Store the image by calling savefig to create a bitmap file on disk:
fig.savefig('test.png')
-
1
Tips & Warnings
To create files that the Python interpreter can read, you must use an ascii text or code editor that creates text-only files.
You can store graph images in many different image formats including: png, pdf, ps and svg.
Some aspects of the matplotlib library installation and functionality vary on different computer platforms. Read the documentation carefully.
The library is capable of displaying numerical information in a vast number of ways and can be finely customized.
References
- "Learning Python, 3rd Edition"; Mark Lutz; 2007
- Introductory matplotlib tutorial
Resources
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images
