enter image description here I am studying Python matplotlib. I have txt file which is including 4 columns.
But I would like to select column 3rd or 4th one from txt.
I tried and studied but there are so many errors in my coding. I am a beginner of python programming so it is too hard to handle by myself. Could you help me, please?
Date | Time | distance | speed
2016/08/25 02:19:39 0.0006 0.6406
2016/08/25 02:19:40 0.0013 2.7856
2016/08/25 02:19:40 0.0019 2.4938
2016/08/25 02:19:42 0.0025 2.1624
2016/08/25 02:19:43 0.0031 1.7867
2016/08/25 02:19:45 0.0038 1.2161
2016/08/25 02:19:50 0.0044 0.4524
2016/08/25 02:19:51 0.0050 1.7881
2016/08/25 02:19:54 0.0057 0.7540
2016/08/25 02:19:55 0.0063 2.7822
And I want to make a graph that x axis is Date and time, and y axis is for distance or speed.
I found this source from internet. And This bottom of source is working for with test.txt.
Date | Time | distance
2016/08/26 23:45:30 0.0088
2016/08/26 23:45:35 0.0094
2016/08/26 23:45:36 0.0101
2016/08/26 23:45:38 0.0107
2016/08/26 23:45:39 0.0113
2016/08/26 23:45:42 0.0119
2016/08/26 23:45:47 0.0126
2016/08/26 23:45:48 0.0132
2016/08/26 23:45:50 0.0138
2016/08/26 23:45:51 0.0145
2016/08/26 23:45:52 0.0151
2016/08/26 23:45:54 0.0157
code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np
# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%Y/%m/%d %H:%M:%S'))
# Read data from 'file.dat'
dates, levels = np.genfromtxt('sss.txt', # Data to be read
delimiter=19, # First column is 19 characters wide
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
unpack=True) # Unpack to several variables
fig = plt.figure()
ax = fig.add_subplot(111)
# Configure x-ticks
ax.set_xticks(dates) # Tickmark + label at every plotted point
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d %H:%M'))
ax.set_ylabel('y')
ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('How many km does my hamster runs?')
ax.set_ylabel('Distance (km)')
ax.grid(True)
# Format the x-axis for dates (label formatting, rotation)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
fig.show()
Here is a working example of how you can do it. The data in data.txt
is as in your first example. I use np.loadtxt to load the text file. The optional arguments I raise are: 1) unpack=True
, so that you get the data into different variables as shown in my example; 2) skiprows=2
to not read the file header; 3) dtype='string'
to parse the data as string. Loading the data as strings forces you to convert the data into either floats or date-time objects. When the load is done it is a simple manner of just plotting the data with matplotlib. For clarity I used twinx
to share the x-axis since the x-values are the same. Does this solve your question?
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
plt.close('all')
filepath = 'data.txt'
date, ttime, dist, vel = np.loadtxt(filepath, unpack=True, skiprows=2, dtype='string')
timestamps = [datetime.strptime(ddate+'-'+tt, '%Y/%m/%d-%H:%M:%S') for ddate, tt in zip(date, ttime)]
dist = map(float, dist)
vel = map(float, vel)
fig, ax_dist = plt.subplots()
ax_vel = ax_dist.twinx()
ax_dist.plot(timestamps, dist, 'r')
ax_vel.plot(timestamps, vel, 'g')
ax_dist.set_ylabel('Distance')
ax_vel.set_ylabel('Velocity')
ax_dist.set_xlabel('Time')
fig.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.