Skip to content

Instantly share code, notes, and snippets.

@calumroy
Last active October 11, 2017 02:05
Show Gist options
  • Save calumroy/70d3ddff56e79f8e4dfe to your computer and use it in GitHub Desktop.
Save calumroy/70d3ddff56e79f8e4dfe to your computer and use it in GitHub Desktop.
Plotting pandas in matplotlib or plotly

Beautiful plots are possible with pandas and matplotlib

Multiple data can be plotted on the same graph with different y axis scales. Below is a sample code where data is pulled from a csv gtab file and loaded into pandas dataframe structures. Matplotlib is then used to plot four sets of data.

import pandas as pd
import matplotlib.pyplot as plt
import scipy.fftpack
import numpy as np
from pandas import DataFrame


nautiData = pd.read_csv(r'./NC_2P8m_150924083719_roll valv not working 3.bin.tab', delimiter='\t')
#print list(nautiData.columns.values)
#print(nautiData)

fig, ax = plt.subplots()

# Twin the x-axis twice to make independent y-axes.
# Make 4 axis for 4 plots with different independent y axis scales.
axes = [ax, ax.twinx(), ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

for axis_index in range(len(axes)):
    if axis_index >= 2: 
        # Move the last y-axis spine over to the right by 20% of the width of the axes
        axes[axis_index].spines['right'].set_position(('axes', 1.0 + (axis_index-1) * 0.1))

        # To make the border of the right-most axis visible, we need to turn the frame
        # on. This hides the other plots, however, so we need to turn its fill off.
        axes[axis_index].set_frame_on(True)
        axes[axis_index].patch.set_visible(False)

# And finally we get to plot things...
colors = ('Green', 'Red', 'Blue', 'yellow')

logtime = nautiData['logtime']
data = [nautiData['rollactive']]
data.append(nautiData['rrdbp'])
data.append(nautiData['rldbp'])
data.append(nautiData['rdbx'])

units = ['mA', 'bar', 'bar', 'mm']

axes[0].plot(logtime, data1, marker='', linestyle='-', color=colors[0])
axes[1].plot(logtime, data2, marker='', linestyle='-', color=colors[1])
axes[2].plot(logtime, data3, marker='', linestyle='-', color=colors[2])
axes[3].plot(logtime, data4, marker='', linestyle='-', color=colors[3])

for axis_index in range(len(axes)):
    axes[axis_index].set_ylabel('%s %s' % (data[axis_index].name, units[axis_index]), color=colors[axis_index])
    axes[axis_index].tick_params(axis='y', colors=colors[axis_index])
axes[0].set_xlabel('time seconds')

plt.show()

@calumroy
Copy link
Author

activeroll_db_pressures_dbx_plot

@calumroy
Copy link
Author

calumroy commented Oct 11, 2017

Plotly Is better

Interactive plots,

from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()
print __version__ # requires version >= 1.9.0

X1 = logtime[1:]
X2 = logtime[1:]
a = steering_angle[1:]
b = INS_Yaw_rate_filtered

# Make scatter trace object with data
trace1 = Scatter(x=X1, y=Y2, name='Y2', mode='lines', yaxis='y')
trace2 = Scatter(x=X1, y=shifted_Y2, name='shifted_Y2',mode='lines', yaxis='y2')
trace3 = Scatter(x=logtime, y=Y1, name='Y1',mode='lines', yaxis='y3')
data = Data([trace1, trace2,trace3])
yaxis = YAxis(title='norm')
yaxis2=YAxis(title='normalized', overlaying='y', side='right',  position=1)
yaxis3=YAxis(title='degs', overlaying='y', side='right',  position=0.92)

layout = Layout(title=log_name,
                xaxis1=xaxis,
                yaxis = yaxis,
                yaxis2 = yaxis2,
                yaxis3 = yaxis3
                )

fig = Figure(data=data, layout=layout)

iplot(fig)

plotly_ex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment