Skip to content

Instantly share code, notes, and snippets.

@calumroy
Last active November 15, 2019 03:32
Show Gist options
  • Save calumroy/dc72053697c1e2632e6b5a41bb6a0765 to your computer and use it in GitHub Desktop.
Save calumroy/dc72053697c1e2632e6b5a41bb6a0765 to your computer and use it in GitHub Desktop.

Plot ros1 topics with pandas and plotly

import math
import pandas as pd
from pandas import *
import matplotlib.pyplot as plt
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
#from plotly.offline import *
from plotly.graph_objs import *

init_notebook_mode()

import pandas as pd
import sqlite3

import rosbag_pandas

Ploting

import plotly.graph_objs as go

# Needs plotly > 3.0.0


var_names = ['Unnamed: 0','/pacmod/as_rx/steer_cmd/command', '/pacmod/parsed_tx/steer_rpt/command',
            '/current_pose/pose/orientation/w', '/pacmod/as_rx/brake_cmd/command', '/current_pose/pose/orientation/x',
            '/current_velocity/twist/linear/x', '/pacmod/parsed_tx/steer_rpt/output']

plot_data = df[var_names]
#plot_data = plot_data.dropna(axis=0)
display(plot_data.loc[plot_data[var_names[2]].notnull()][var_names[2]].values)

# Plot each of the variables in the var_names list
data = [
    Scatter(
        x=plot_data.loc[plot_data[var_names[i]].notnull()][var_names[0]].values,
        y=plot_data.loc[plot_data[var_names[i]].notnull()][var_names[i]].values,
        name=var_names[i]#,
        #type='scattergl'
        #yaxis="y{x}".format(x=(i) if i!=0 else "")
    ) for i in range(1,len(var_names))
]


# Set the layout
xaxis = go.layout.XAxis(title='Seconds')
layout = Layout(title='Pacmod Signals'
                )

# Set the number of yaxis
yaxis = [go.layout.YAxis(title='mm'),
         go.layout.YAxis(title='Amps', overlaying='y1', side='left',  position=0.94),
         go.layout.YAxis(title='V', overlaying='y1', side='right',  position=0.9),
         go.layout.YAxis(title='mm', overlaying='y1', side='right',  position=0.8)
         
        ]

for ind in range(len(yaxis)):
    layout['yaxis'+str(ind+1)] = yaxis[ind]
 
# Set the plots to specfic yaxis numbers
data[0].yaxis = 'y1'
data[1].yaxis = 'y1'
# data[2].yaxis = 'y3'
# data[3].yaxis = 'y2'

fig_widget = go.FigureWidget(data, layout=layout)
fig_widget
# Convert a ROSBag to a dataframe
# Note large rosbags will cause an out opf memory failure
# use this command from rosbag_pandas to select a topic and output a csv
#bag_csv -b 2019-10-24-16-08-28.bag -i /current_pose -o 2019-10-24-16-08-28.csv
# We can also plot with 
# bag_plot -b 2019-10-24-16-08-28.bag -k /current_pose/pose/orientation/x

df = rosbag_pandas.bag_to_dataframe('./EMBOTECH_ALV003_2019-11-14-16-48-13.bag')
#df = pd.read_csv(r'./2019-10-24-16-08-28.csv', delimiter=',')
display(df.columns.values)
display(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment