Live Graph Simulation using Python, Matplotlib and Pandas

Ujwal Tewari
Intel Student Ambassadors
5 min readApr 22, 2020

Make live graphs with dynamic line, scatter and bar plots. Also learn to plot graphs in 3D and 2D quickly using pandas and csv.

Pandas and Matplotlib are very useful libraries when it comes to graph plotting and circulation. Often it becomes quite time consuming when you have collected chunks of data but have to separately search for plotting tools to visualize your data. Pandas, coupled with matplotlib offers seamless visualization of data directly from csv files.
In this post we will learn how to plot various plots directly from csv files and then later how to present the data in a live moving and dynamic graph. Further as a bonus, plotting the data in 3D shall also be described.

Lets begin coding folks

Plot directly from CSV files:

Let us begin with the basics which is by plotting different types of plots directly from csv files.

import pandas as pd
import matplotlib.pyplot as plt

We import the two essential packages required for the plots. For the purpose of demonstration we shall be using the csv file available here : Link . We shall use sales records as it it more data centric. (CSV file)

med_path = "D:/FL_insurance_sample.csv/SalesJan2009.csv"
med = pd.read_csv(med_path)
sales = pd.DataFrame(med)
sales

We define the path of the csv file and then using pandas read the file. Next we convert it to a pandas dataframe which makes easier to play with data. Then we visualize to see what data are we dealing with which looks something like this:

Jupyter notebook offers more flexibility in coding

Next step is to plot the data. We start with a line chart with the following snippet:

ax =plt.gca()
sales.plot(kind=’line’,y = ‘Latitude’,ax=ax,color=’red’)
ax.set_xlabel(“Index values”)
ax.set_ylabel(“Latitude values”)
plt.title(‘Demo graph for Line plots’)
plt.show()

plt.subplots(dpi=120) gives the figure and axes for the plots while dpi helps us decide the quality of the graph image. We describe the type of graph in kind variable and then later set x and y labels along with the plot title. We have the following output:

Linear plot for just one entity

We can plot multiple entities by just adding more plot values along with legends for the same as shown below:

Keep separating the color for each plot for better visualization

Similarly we also visualize scatter plot-single plot using the following snippet:

fig, ax = plt.subplots(dpi=120)
sales.plot.scatter(y='Country', x='Payment_Type', color='red', ax=ax);
ax.set_xlabel("Card types")
ax.set_ylabel("Countries")
plt.title('Demo graph for Scatter plot')
plt.show()
Single variable scatter plot

We then extend for multi scatter plot in the same figure as follows:

Multi variable scatter plot

The data file chosen isn't very suitable for some types of plots so we use random values for better visualization for those types of plots.

Bar Plots:

fig, ax = plt.subplots(dpi=120)
random_values = pd.DataFrame(np.random.rand(10, 4), columns=[‘a’, ‘b’, ‘c’, ‘d’])
random_values.plot(kind=’bar’,ax=ax)
ax.set_xlabel(“Colums”)
ax.set_ylabel(“Data values”)
plt.title(‘Demo graph for Bar plot’)
plt.show()
Multi bar Plots in one figure

Area Plot:

fig, ax = plt.subplots(dpi=120)
area = pd.DataFrame(np.random.rand(10, 4), columns=[‘a’, ‘b’, ‘c’, ‘d’])
area.plot(kind=’area’,ax=ax)
plt.title(‘Demo graph for Area plot’)
plt.show()
Two different Areas plots with stacked True and False for better clarification of data

These were some of the most commonly used plots for data visualization. Now we move on to plotting these plots as live and dynamic data

No long codes so don’t worry. The best is yet to come

Live — Dynamic Graphs:

We will be using FuncAnimation function in matplotlib for this purpose which makes animation by repeatedly calling a function *func*.
We define the necessary imports and initializations necessary for the code execution here:

import random
from itertools import count
import time
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits import mplot3d
plt.style.use(‘fivethirtyeight’)x_values = []
y_values = []
z_values = []
q_values = []
counter = 0index = count()

We shall be plotting three dynamic line plots in the same figure and will randomly pool these values from a specified range

Firstly we define the *func* function to be utilized here:

The comments in the code make it relatively easier to understand the concept that animate function keeps calling the function defined by us and plt.cla keeps refreshing the plot which makes it dynamic. Finally we call the above function to see the dynamic graphs-
fig, ax = plt.subplots()

ani = FuncAnimation(plt.gcf(), animate, 1000)
plt.tight_layout()
plt.show()

Now we shall see how it actually looks when we run the code->

Live dynamic line plots for three different values

Note — The animate function is a memory less function hence will not perform operations like counter = counter+1

Simply use different plot types as mentioned earlier to create live-scatter/bar/area graphs

Thank you Pandas and Matplolib for existing and making lives of Data scientists easier.

Conclusion:

Hence in this post we learned how to code multiple types of plots using pandas and matplotlib. We learned how to read from csv file, convert them into pandas’ data-frames and plot respective graphs within a single line of code. In addition to all we also learned to code dynamic and moving graphs and are now ready to make dazzling presentation and beautiful data visualizations. Most importantly we learned the flexibility available within pandas and matplolib when it comes to data visualization.

Bonus Section:

In the bonus section we will breifly go over how to create 3D plots. We will plot the same line and scatter plots that we plotted earlier but in 3D.
We just add one additional import :

from mpl_toolkits.mplot3d import Axes3D

which is for importing matplotlib for 3D projections.

First we shall directly plot the previous graphs without any addition of z-axis exclusively and the results would be:

Plots are in 3D space but are missing the third dimension in data.

Now we shall add some noise/random values as the third dimension to add depth in the data visualization part ->

Depth based 3D line plots

Code is relatively simple and easy to understand as shown below:

Happy coding the live and 3D plots guys

Viola we have done this which also marks the end of this blog post.

--

--

Ujwal Tewari
Intel Student Ambassadors

Senior Research Scientist @Games24x7 | Intel AI innovator | Udacity DRL mentor | ML & AI blogger