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 because of their simplicity and portability, they are ineffective for displaying or analyzing large amounts of data. Using Python and one external code library, matplotlib, a programmer can overcome this limitation by converting the raw CSV data into a readable, visually attractive graph suitable for web or print publication.


Video of the Day

Rendering a CSV File as a Graph Using Python and Matplotlib


Create a simple CSV file for testing. A sample might look like this:

1,2 2,3 3,8 4,13 5,18 6,21 7,13 7.5,4 2.5,4.3


Import the necessary python libraries into your code file:

import matplotlib.pyplot as plt import csv import sys


Open the CSV file and create a reader object from it. Declare variables to define the upper and lower bounds for the x and y axis values of the graph:

csv_reader = csv.reader(open('test.csv')) bigx = float(-sys.maxint -1) bigy = float(-sys.maxint -1) smallx = float(sys.maxint) smally = float(sys.maxint)


Iterate over each row contained in the reader object storing each row as a vertex in a vertex array. In the same loop compare the x and y values in order to store their upper and lower bounds. Sort the vertex array and then loop through it again. This time store the sorted x and y values in separate arrays:

verts = [] for row in csv_reader: verts.append(row) if float(row[0]) > bigx: bigx = float(row[0]) if float(row[1]) > bigy: bigy = float(row[1]) if float(row[0]) < smallx: smallx = float(row[0]) if float(row[1]) < smally: smally = float(row[1]) verts.sort() x_arr = [] y_arr = [] for vert in verts: x_arr.append(vert[0]) y_arr.append(vert[1])


Create a FigureCanvas object using the imported matplotlib pyplot object. 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 —they can range from 0.0 to 1.0:

fig = plt.figure() 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:

ax.set_xlabel('x data') ax.set_ylabel('y data') ax.set_xlim(smallx,bigx) ax.set_ylim(smally,bigy)


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). Display the finished graph by calling the show method to open a window and store the image by calling savefig to create a bitmap file on disk:

ax.plot(x_arr,y_arr, color='blue', lw=2) fig.savefig('test.png')

Show Comments