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.
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:
- "Learning Python, 3rd Edition"; Mark Lutz; 2007
- Introductory matplotlib tutorial
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images
How to Convert a CSV File to a Graph in Python
Most database and spreadsheet applications can conveniently output table data in the form of CSV (comma-separated-values) files. While CSV files are handy...
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 Create a Floating Bar Graph
Bar graphs are a method of visually presenting numerical data in a way that is accessible to a wider range of people...
How to Graph a CSV File in Excel
Excel is a versatile spreadsheet application made by Microsoft for the Windows and Mac OSX operating systems. It features graphing, calculation and...
How to Manipulate XLS Files With Python
Python users are not given a single, unified interface for interacting with Excel spreadsheet files. Instead, there are two different libraries: xlrd...
About Monty Python and the Holy Grail
Originally released in 1975, the film "Monty Python and the Holy Grail" is a silly lampoon of medieval life, politics, religion, and...
How To Use the Probability Function In 2007 Excel
The probability, or PROB, function in Microsoft Excel 2007 takes four arguments and returns the probability that the values of cells in...
Open Source Web-Based Project Management Tools
Open Source Web-Based Project Management Tools. Managing large projects that involve diverse groups of people can be challenging. High end project management...
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...
How to Create an Encrypted Disk Image
With the Disk Utility application, which comes installed on all new Mac computers, you can create disk images to condense files or...
Introduction to Combinatorics
Combinatorics is an area of mathematics that deals with the study of combination, enumeration, and permutations of sets of elements. It can...
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 Python Habitat
Snakes are expensive pets. The python itself is usually inexpensive-but all the equipment you need to keep the snake alive is not....
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...