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
Creating a Chart Using Python
Import the matplotlib pyplot module into the code file:
import matplotlib.pyplot as plt
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
Declare arrays to separately hold x and y values to be plotted:
x_arr = 
y_arr = 
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):
Create a FigureCanvas object using the imported matplotlib pyplot object:
fig = plt.figure()
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])
Format the graph, adding labels and defining the minimum and maximum values for each axis:
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)
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:
Store the image by calling savefig to create a bitmap file on disk:
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.
- "Learning Python, 3rd Edition"; Mark Lutz; 2007
- Introductory matplotlib tutorial
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images
How to Code in Python
Python is an interpreted scripting language with simple, readable code intended to be intuitive and even fun to use --- hence its...
How to Create an XY Chart
Often used for analyzing engineering, scientific and statistical data, the XY chart can also help anyone forecast trends by tracking factual data...
How to Make a Graph in MS Excel
Microsoft Excel makes it easy to make a graph. All you need to do is supply the data for the graph and...
Watch as a seasoned professional discusses types of guppy food in this free online video about exotic pets.
How to Write Computer Code
Learning to write computer code will require the use of a program called Python, which has very easy language to learn. Discover...
How to Make a Line Graph in Microsoft Excel
Microsoft Excel makes it easy to make a line graph. All you need to do is supply the data for the line...
How to Create Matrices in Powerpoint
Microsoft PowerPoint is a presentation tool that is part of the Microsoft Office software suite and allows users to create various charts,...
How to Create a While Loop in Python
Computers are much better than people at performing repetitive tasks over and over. You may have a large (and sometimes infinite) list...