Python graph from txt(csv) file and there are 5 columns, How Can I pick 3rd or 4th columns for y axis?
<p><a href="http://i.stack.imgur.com/BCfNp.png" rel="nofollow">enter image description here</a>I am studying Python matplotlib.
I have txt   file which is including 4 columns.</p>

<p>But I would like to select column 3rd or 4th one from txt.</p>

<p>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?</p>

<pre><code>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  
</code></pre>

<p>And I want to make a graph that x axis is Date and time,
and y axis is for distance or speed.</p>

<p>I found this source from internet.
And This bottom of source is working for with test.txt.</p>

<pre><code>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></pre>

<p>code:</p>

<pre><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()
</code></pre>

<p>Here is a working example of how you can do it.  The data in <code>data.txt</code> is as in your first example.  I use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html" rel="nofollow">np.loadtxt</a> to load the text file.  The optional arguments I raise are: 1) <code>unpack=True</code>, so that you get the data into different variables as shown in my example; 2) <code>skiprows=2</code> to not read the file header; 3) <code>dtype='string'</code> 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 <code>twinx</code> to share the x-axis since the x-values are the same.  Does this solve your question?</p>

<pre><code>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()
</code></pre>

python mysql date matplotlib graph