Excercise: Try plotting the data from csv file using python and save the plot to a jpeg or png file, which we can later make it available for other apps.
csvfile name: test.csv
Location: Current working directory
Python version: 2.7.5
Modules used: matplotlib, numpy
Code:
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
data = np.genfromtxt("test.csv", delimiter=",", names=["x", "y"])
plt.plot(data['x'], data['y'])
plt.savefig('/tmp/test1.png')
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
data = np.genfromtxt("test.csv", delimiter=",", names=["x", "y"])
plt.plot(data['x'], data['y'])
plt.savefig('/tmp/test1.png')
Code explanation:
numpy is needed to read the file in this example
matplotlib is needed for plotting the chart
matplotlib.use('Agg') is needed to supress the requirement of working DISPLAY variable
matplotlib is needed for plotting the chart
matplotlib.use('Agg') is needed to supress the requirement of working DISPLAY variable
csv content:
1,2
2,5
3,7
4,7
5,9
6,10
7,20
8,21
9,22
10,30
2,5
3,7
4,7
5,9
6,10
7,20
8,21
9,22
10,30
Output:
This comment has been removed by a blog administrator.
ReplyDelete