Plotting more than two columns in python using Panda from a CSV file
<p>I am very new to python and coding in general. I have a CSV file that has the first row a string of flight attributes (ie. time, speed, heading, battery voltage etc.) I want to be able to plot time vs speed vs battery voltage. Or battery voltage vs speed. I have tried several examples, however, every example I have seen it is either plots everything in the CSV file which I do not want. Or the csv file only has 2 columns.</p>

<pre><code>CSV File format:
time(ms), ground_speed, battery_voltage(v), airspeed(m/s)
1322,45,12,42
1342,64,11,60
1352,30,11,27

import pandas as pd
from pandas import DataFrame, read_csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')


df = pd.DataFrame.from_csv('myflight.csv', index_col=False)

df.plot(x='mSec', y='Airspeed(m/s)', color="g")
df.plot(x='mSec', y='AS_Cmd(m/s)', color="r")
</code></pre>

<p>My issue with this it always plots vs the first index which is time. Also, I get two separate graphs. How do I join both airspeed and AS_Cmd into one graph and be able to plot someone else beside time vs something else?</p>

<p>As you're new with python you probably should start slower and try to mess around with <code>numpy</code> first. <code>pandas</code> is a library build around <code>numpy</code>, which expects you to be familiar with that.</p>

<p>We can simplify your code a bit. I've added some comments to guide you through.</p>

<pre><code># You do not need that many import statements, so we just import
# numpy and matplotlib using the common alias 'np' and 'plt'.
import numpy as np
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')

# Using numpy we can use the function loadtxt to load your CSV file.
# We ignore the first line with the column names and use ',' as a delimiter.
data = np.loadtxt('myflight.csv', delimiter=',', skiprows=1)

# You can access the columns directly, but let us just define them for clarity.
# This uses array slicing/indexing to cut the correct columns into variables.
time = data[:,0]
ground_speed = data[:,1]
voltage = data[:,2]
airspeed = data[:,3]

# With matplotlib we define a new subplot with a certain size (10x10)
fig, ax = plt.subplots(figsize=(10,10))

ax.plot(time, ground_speed, label='Ground speed [m/s]')
ax.plot(time, voltage, label='Voltage [V]')

# Show the legend
plt.legend()
</code></pre>

<p>This code will get you this graph here:</p>

<p><a href="http://i.stack.imgur.com/tilGv.png" rel="nofollow"><img src="http://i.stack.imgur.com/tilGv.png" alt="enter image description here"></a></p>

<h2>References</h2>

<p>You can find the documentation to the functions used in the <a href="http://docs.scipy.org/doc/numpy/reference/index.html" rel="nofollow">NumPy reference</a>.</p>

<ul>
<li><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html" rel="nofollow">numpy.loadtxt</a></li>
<li><a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html" rel="nofollow">Indexing</a></li>
</ul>

<h2>Update</h2>

<p>To clarify on the use of <code>plt.subplots(figsize=(10,10))</code>: You create a new figure with this command, but matplotlib wants to return several values (two in this case). Therefore you need to specify two variables to accept those. <code>fig</code> saves the Figure instance, while <code>ax</code> will save the current <code>subplot</code>.
A figure can have an unlimited amount of subplots, so you can do fancy things as shown <a href="http://www.scipy-lectures.org/intro/matplotlib/matplotlib.html#subplots" rel="nofollow">here</a>. In the example above we are only defining one subplot.</p>

<p>The <code>figsize</code> attribute defines the size of the Figure in inches, thus the resulting output is 10 x 10 inches. You can play around with the values and look at the outputs.</p>

python pandas plot