Mike Jones
Mike Jones

Reputation: 53

Plotting more than two columns in python using Panda from a CSV file

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.

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")

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?

Upvotes: 2

Views: 5420

Answers (3)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

You can simply set your desired index and plot everything in one shot:

import matplotlib

matplotlib.style.use('ggplot')

df.set_index('time(ms)')[['ground_speed','battery_voltage(v)']].plot(rot=0, figsize=(16,10))

enter image description here

Upvotes: 2

Michael Gecht
Michael Gecht

Reputation: 1444

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

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

# 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()

This code will get you this graph here:

enter image description here

References

You can find the documentation to the functions used in the NumPy reference.

Update

To clarify on the use of plt.subplots(figsize=(10,10)): 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. fig saves the Figure instance, while ax will save the current subplot. A figure can have an unlimited amount of subplots, so you can do fancy things as shown here. In the example above we are only defining one subplot.

The figsize 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.

Upvotes: 2

Khris
Khris

Reputation: 3212

To directly solve your problem without going deeper just do this:

subp = plt.figure().add_subplot(111)
df.plot(x='mSec', y='Airspeed(m/s)', color="g", ax=subp)
df.plot(x='mSec', y='AS_Cmd(m/s)', color="r", ax=subp)

You need to define a common object which to plot your plots on and give it to your plot commands, otherwise each time you plot a new one is created.

Upvotes: 1

Related Questions