Python plot dates using matplotlib - python

I'm very beginner at Python and matplotlib but trying to learn! I would like to use matplotlib to plot some simple data from a CSV containing dates with a frequency. The X axis containing dates and Y containing the frequency. Example data from CSV:
2011/12/15,5
2011/12/11,4
2011/12/19,2
I checked the "matplotlib.sf.net/examples" out but appears all the test data is downloaded from a http get. I would really appreciate if someone could guide me with some example code of how to read in (presumably using CSV reader) and display data in chart.
Thank you!!

Maybe you look for something like:
import csv
import datetime as dt
import matplotlib.pyplot as plt
arch = 'C:\\Python26\\programas\\test.csv'
data = csv.reader(open(arch))
data = [(dt.datetime.strptime(item, "%Y/%m/%d"), float(value)) for item, value in data]
data.sort()
[x, y] = zip(*data)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.grid(True)
fig.autofmt_xdate()
plt.show()

I've tried to keep my code as simple as possible and this is by no means elegant, but here you go:
import csv
import matplotlib.pyplot as plt
### Making test CSV file ###
data = [['2011/12/15,5'],['2011/12/11,4'],['2011/12/19,2'],['2011/12/16,3'],['2011/12/20,8'],['2011/12/14,4'],['2011/12/10,10'],['2011/12/9,7']]
with open('test.csv', 'wb') as f:
writer = csv.writer(f)
for i in data:
writer.writerow(i)
### Extract data from CSV ###
with open('test.csv', 'rb') as n:
reader = csv.reader(n)
dates = []
freq = []
for row in reader:
values = row[0].split(',')
dates.append(values[0])
freq.append(values[1])
### Do plot ###
false_x = [x for x in range(len(dates))]
plt.plot(false_x,freq, 'o-')
plt.xticks(range(len(dates)), (dates), rotation=45)
# plt.axis([xmin, xmax, ymin, ymax]) - sets axes limits on graph
plt.axis([-1, 8, 0, 11])
plt.show()
This makes:

Related

Outputting data to csv in python

I have data that this script outputs as a scatterplot of ID_all[:,0],ID_all[:,1], but I want to also save the data used for the scatter plot as two columns (as a csv file). Any help really appreciated! Thanks in advance.
More Info:
Here is a link to the initial csv data so that you can see my problem (only first two columns used).
https://www.dropbox.com/s/966iviv01tccv1k/contacts.csv?dl=0
If you plot the input csv file (see figure 1), you will see that it only has half of the values that I need. So when I plot this with the below script, I transpose the 'matrix', making a mirror image of this vrt the main diagonal. Now with this script I plot both the original and the transpose (figure 2), but I somehow cannot figure out how to just output columns of data in addition to the figure.
import numpy as np
import matplotlib.pyplot as plt
def parsefile(filename):
i = 0
print('Opening File')
try:
for line in open(filename):
line = line.split()
ID = np.hstack((int(line[0]), int(line[1])))
if i==0:
ID_list = ID
i+=1
if i>0:
ID_list = np.vstack((ID_list, ID))
print('File Opened')
return ID_list
except:
return ID_list
def get_scatter_data(ID_list):
ID_swapped1 = ID_list[:, 1]
ID_swapped2 = ID_list[:, 0]
ID_swapped = np.transpose([ID_swapped1.T, ID_swapped2.T])
N = np.max(ID_list)
diagonal = np.linspace(1,N,N)
ID_diagonal = np.transpose([diagonal.T,diagonal.T])
ID_all = np.vstack((ID_list,ID_swapped,ID_diagonal))
return ID_all
def plot_contactmap(ID_all, savefilename):
f = plt.figure()
plt.scatter(ID_all[:,0],ID_all[:,1], marker='o', s=0.01)
plt.axis('square')
plt.xlabel('Residue Number', fontsize = 14)
plt.ylabel('Residue Number', fontsize = 14)
plt.tick_params(axis='both', which='major', labelsize=14)
plt.show()
f.savefig(savefilename+'.png', bbox_inches='tight')
f.savefig(savefilename+'.pdf', bbox_inches='tight')
def main(filename, savefilename):
ID_list = parsefile(filename+'.csv')
ID_all = get_scatter_data(ID_list)
plot_contactmap(ID_all, savefilename)
main('contacts', 'test')
You can save using numpy.savetxt() method
import numpy as np
ID_all = np.array([[1,2],[3,4],[5,6]])
np.savetxt('output.csv', ID_all, delimiter=',', fmt="%d")
If you want to give column names you can use header argument np.savetxt() method.
np.savetxt('output.csv', ID_all, delimiter=',', header='first, second', fmt="%d")

Labelling multiple points from CSV in basemap

I'm having issues labeling all the points plotted with the code below,
each row in the CSV file is name, lat,lon.
Points are plotting just fine.
Everything I've tried to have names be plotted along x,y has borked everything in one way or another.
Anyone have any tips?
with open('/.../geo/ALL.csv') as csvfile:
reader = csv.reader(csvfile,delimiter=',')
for data in reader:
names.append(str(data[0]))
lats.append(float(data[1]))
lons.append(float(data[2]))
x,y = map(lons,lats)
map.plot(x,y,'r*',markersize=0.02,color='yellow',marker='D')
You goal is to plot (lons,lats) points in the xy-plane with a label on each. This is one way to do it:
import csv, matplotlib.pyplot as plt
names, lats, lons = [], [], []
with open('file.csv') as csvfile:
reader = csv.reader(csvfile,delimiter=',')
for data in reader:
names.append(str(data[0]))
lats.append(float(data[1]))
lons.append(float(data[2]))
fig, ax = plt.subplots()
ax.scatter(lats, lons, marker='*', s=400) # play with 's' to control the dot size
pad = " " # play with this to pad the label from the dot
for i, n in enumerate(names):
ax.annotate(pad+str(n), (lats[i], lons[i]))
plt.savefig("scatter.png") # you will find scatter.png file in the same directory
This is the scatter.png I get:
with this file.csv CSV file:
n1,1,8
n4,7,2
n2,3,14
n7,13,4
n5,9,6
n6,11,12
n3,5,10
n8,15,18
n9,17,16

How can I convert det unix time values from txt file to date values using matplotlib?

I basically want to convert the unix time values I have in the first row of my text file named 'EKTE9' to date. I think using the datetime library is the way to go, but I don't know how to implement that in my code. Some answers would be appreciated.
import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('EKTE9.txt','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(int(row[0]))
y.append(float(row[3]))
plt.plot(x,y, label='Temperatur')
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()
Here are some values from the EKTE9.txt file:
1554058225,0.80,2.90,13.60,27.20
1554058525,0.30,0.80,9.60,26.70
The values goes on for about 200 lines
That did the job. Thank you very much Martin Evans.
Fina code:
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
import csv
x = []
y = []
with open('EKTE9.txt','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(matplotlib.dates.date2num(datetime.fromtimestamp(int(row[0]))))
y.append(float(row[3]))
hfmt = matplotlib.dates.DateFormatter('%d\n%H:%M')
plt.plot(x,y, label='Temperatur')
plt.gca().xaxis.set_major_formatter(hfmt)
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()
You would first create a datetime object using datetime.fromtimestamp(). This can then be converted to a matplotlib number using date2num(). Finally, you should use a DateFormatter() to help with displaying the x-axis.
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime
import csv
x = []
y = []
with open('EKTE9.txt', 'r', newline='') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(matplotlib.dates.date2num(datetime.fromtimestamp(int(row[0]))))
y.append(float(row[3]))
hfmt = matplotlib.dates.DateFormatter('%d\n%H:%M')
plt.plot(x,y, label='Temperatur')
plt.gca().xaxis.set_major_formatter(hfmt)
plt.xlabel('Tid')
plt.ylabel('Temperatur')
plt.title('Grafen viser temperatur under forsøket\n')
plt.legend()
plt.show()
This would then show your two values as follows:

Plotting Time and float value using python matplotlib from File

I am having a text file with time and a float value. I have heard that it is possible to plot these two columns using matplotlib. Searched similar threads but could not make it happening. My code and Data are-
import math
import datetime
import matplotlib
import matplotlib.pyplot as plt
import csv
with open('MaxMin.txt','r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True)
x = []
y = []
for cols in csv_input:
x = matplotlib.dates.date2num(cols[0])
y = [float(cols[1])]
# naming the x axis
plt.xlabel('Real-Time')
# naming the y axis
plt.ylabel('Acceleration (m/s2)')
# giving a title to my graph
plt.title('Accelerometer reading graph!')
# plotting the points
plt.plot(x, y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
# function to show the plot
plt.show()
And part of the Data in MaxMin.txt
23:28:30.137 10.7695982757
23:28:30.161 10.4071263594
23:28:30.187 9.23969855461
23:28:30.212 9.21066485657
23:28:30.238 9.25117645762
23:28:30.262 9.59227680741
23:28:30.287 9.9773536301
23:28:30.312 10.0128275058
23:28:30.337 9.73353441664
23:28:30.361 9.75064993988
23:28:30.387 9.717339267
23:28:30.412 9.72736788911
23:28:30.440 9.62451269364
I am a beginner in Python and on python 2.7.15 in windows 10 pro(64 bit). I have installed numpy,scipy scikit-learn already. Please help.
Final Output Graph from complete Data Set. Thanks # ImportanceOfBeingErnest
You could use pandas to achieve this, first store your file in a .csv format:
import math
import datetime
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd #### import this library
df = pd.read_csv("path_to_file.csv", delimiter=' ', encoding='latin-1')
x = df.ix[:,0]
y = df.ix[:,1]
# naming the x axis
plt.xlabel('Real-Time')
# naming the y axis
plt.ylabel('Acceleration (m/s2)')
# giving a title to my graph
plt.title('Accelerometer reading graph!')
# plotting the points
plt.plot(x, y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
# function to show the plot
plt.show()
if the first colunm does not have a datatime format you may convert it to this format like df.ix[:,0] = pd.to_datetime(df.ix[:,0])
and you take the hour for example:
df.ix[:,0] = df.ix[:,0].map(lambda x: x.hour)
The output after running the code was like:
The error you made in the original attempt is actually pretty minor. Instead of appending the values from the loop you redefined them.
Also you would need to use datestr2num instead of date2num, because the string read in is not yet a date.
import matplotlib
import matplotlib.pyplot as plt
import csv
with open('MaxMin.txt','r') as f_input:
csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True)
x = []
y = []
for cols in csv_input:
x.append(matplotlib.dates.datestr2num(cols[0]))
y.append(float(cols[1]))
# naming the x axis
plt.xlabel('Real-Time')
# naming the y axis
plt.ylabel('Acceleration (m/s2)')
# giving a title to my graph
plt.title('Accelerometer reading graph!')
# plotting the points
plt.plot_date(x, y)
# beautify the x-labels
plt.gcf().autofmt_xdate()
# function to show the plot
plt.show()
My recommendation for how to make this easier would be, to use numpy and convert the input to datetime.
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
x,y= np.loadtxt('MaxMin.txt', dtype=str, unpack=True)
x = np.array([datetime.strptime(i, "%H:%M:%S.%f") for i in x])
y = y.astype(float)
plt.plot(x,y)
plt.gcf().autofmt_xdate()
plt.show()
Concerning the ticking of the axes: In order to have ticks every half a second you can use a MicrosecondLocator with an interval of 500000.
import matplotlib.dates
# ...
loc = matplotlib.dates.MicrosecondLocator(500000)
plt.gca().xaxis.set_major_locator(loc)
plt.gca().xaxis.set_major_formatter(matplotlib.dates.AutoDateFormatter(loc))

Reading list from CSV and convert to float list

I want to do some math on my data and stuck with it.
I am reading string lists from csv files and can plot them so far.
Now I am trying to convert the string lists to float lists as i want to do some math on the list items and create new lists and plot them.
import numpy as np
import math
import matplotlib.pyplot as plt
with open("testlog.csv") as f:
data = f.read()
data = data.split('\n')
time = [row.split(',')[0] for row in data]
gyro_x_lsb = [row.split(',')[1] for row in data]
gyro_y = [row.split(',')[2] for row in data]
gyro_z = [row.split(',')[3] for row in data]
accel_x = [row.split(',')[4] for row in data]
accel_y = [row.split(',')[5] for row in data]
accel_z = [row.split(',')[6] for row in data]
comp_x = [row.split(',')[7] for row in data]
comp_y = [row.split(',')[8] for row in data]
comp_z = [row.split(',')[9] for row in data]
temp = [row.split(',')[10] for row in data]
gyro_x_lsb = float(gyro_x_lsb)# make floats in a new list
gyro_x_dps = [gyro_x_lsb / (32768*2000) for gyro_x_dps_f in gyro_x_lsb]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title("Gyro X AR [LSB]")
ax1.set_xlabel('Time[ms]')
ax1.set_ylabel('AR [LSB]')
ax1.plot(time,gyro_x_lsb, c='r', label='X')
leg = ax1.legend()
ax2 = fig.add_subplot(212)
ax2.set_title("MPU9250_test_Accel")
ax2.set_xlabel('Time[ms]')
ax2.set_ylabel('Acceleration')
ax2.plot(time,accel_x, c='r', label='Accel_X')
leg = ax2.legend()
plt.show()
I am trying to calculate every item in "gyro_x_lsb"/32768*2000. But it will not work.
I have tried map also already.
Thanks in advance...
TMP36
BTW: I use Anaconda 3(2.5.0) with Python 3.5
I am new to Python and to this forum.
List comprehensions will do a lot for you.
Like:
gyro_x_lsb_sum = sum(float(item) for item in gyro_x_lsb)
Here I am using a generator expression which is the same syntax as a list comprehension, but better performance in this case.
Hope it helps a bit.
PS. Consider using the Python csv module to read CSV files.
Thanks for the hints with the CSV module. This read my data now as tuple. This was also new to me, and I found a possibility to make some math on my data. Here is the code now that works for me, maybe not perfect, but i am happy for now.
import csv
import matplotlib.pyplot as plt
with open("testlog.csv") as data:
#reader = csv.reader(data) #read columns as strings
reader = csv.reader(data, quoting=csv.QUOTE_NONNUMERIC) #read columns as numbers
time, gyro_x, gyro_y, gyro_z, accel_x, accel_y, accel_z, comp_x, comp_y, comp_z, temp = zip(*reader)
#gyro_x_dps = gyro_x
def gyro_x_dps(gyro_x):
return tuple( [e / 65536000 for e in gyro_x])
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title("Gyro X AR [LSB]")
ax1.set_xlabel('Time[ms]')
ax1.set_ylabel('AR [LSB]')
ax1.plot(time,gyro_x, c='r', label='X')
leg = ax1.legend()
ax2 = fig.add_subplot(212)
ax2.set_title("Gyro X AR [dps]")
ax2.set_xlabel('Time[ms]')
ax2.set_ylabel('AR [dps]')
ax2.plot(time,gyro_x_dps(gyro_x), c='r', label='X')
leg = ax2.legend()
plt.show()

Categories

Resources