Convert A Csv File To A Graph In Python

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.

Instructions

Rendering a CSV File as a Graph Using Python and Matplotlib 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])

5. 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])

6. 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)

7. 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)

plt.show()

fig.savefig(‘test.png’)


READ  Specifications For The Emachines Etower 433i