The ability to visualize data is a powerful skill in the world of Business Intelligence. BI is the intelligence that is derived by analysing and studying a huge amount of data, and tabular data is just not enough to represent that intelligence. Graphs, plots and diagrams do a lot more than what pieces of texts or numbers can. If you are a Data Scientist or someone who is just starting the journey, then there is no need to explain the importance and power of data visualization.

Today there are lots of tools, libraries and applications that allow data scientists or business analysts to visualize data in plots or graphs. In this article, we will focus on pandas ‘plot’, which is one of the easiest plotting libraries in Python that allows users to plot data-frames on the go.

REGISTER FOR OUR UPCOMING ML WORKSHOP

### Pandas Plot

The pandas plot is built-off of one of the most widely used plotting libraries, the matplotlib. But pandas plot is essentially made for easy use with the pandas data-frames. When there is one library that does all things with data and data-frames it should also be able to visualize the data, that is what pandas plot is all about.This article will introduce you to this simple package that is available with pandas and to create your first plots.

When it comes to the variety of plots and plotting features, pandas plot cannot be compared to other dedicated plotting libraries but it does come with some impressive and easy to use features that helps us get some insights while working with data on-the-go.

Let us dig into the examples!!

### Plotting With Pandas

Let us begin by creating a very simple data-set like the one shown below. You can also download the sample by clicking here.

Let’s load the data-set into pandas by executing the below code block:

`import pandas as pd`

`data = pd.read_csv("sample_data.csv")`

We are all set to start making some cool plots from the data we have. Lets us do some on-the-go plotting.

The kind keyword argument is used to specify what kind of graph to plot from a given set of ‘line’, ‘bar’, ‘barh’, ‘hist’, ‘box’, ‘kde’, ‘density’, ‘area’, ‘pie’, ‘scatter’ and ‘hexbin’ plots.

### Simple Plots

Line graph:

`data.plot(kind='line')`

Takes all the numerical data in the given data set and plots against each observation in the data by default.Each column is represented in different coloured lines.

Area Plot:

`data.plot(kind='area',stacked = False)`

Takes all the numerical data in the given data set and plots against each observation in the data by default.Each column is represented in different coloured area.

The stacked keyword argument generates a stacked plot. When stacked is True, each column must be either all positive or negative.In the given data all the columns contains both positive and negative values so we disable stacked plot.

Bar Graph:

`data.plot(kind='bar')`

Takes all the numerical data in the given data set and plots a bar for each column against each observation in the data by default. Each column is represented in different coloured bar.

Horizontal Bar Graph;

`data.plot(kind='barh')`

Same as Bar graph but is plotted in alternate axis.

Histogram:

`data.plot(x=None, y='B', kind='hist')`

Plots a histogram for the specified data. In the above code block the histogram is plotted for feature or column ‘B’.

Box Plot:

`data.plot(kind='box')`

Takes all the numerical data in the given data set and plots a box plot for each column by default.

Density Plot:

`data.plot(kind='kde')`

Plots a density for each column by default. Each column is represented in different coloured line.

Scatter Plots

`data.plot(x = 'A', y = 'B', kind='scatter', title = 'Scattered Plots')`

Scatter plot plots the data points in a two dimensional space.

Hexbin Plots

`data.plot(x = 'A', y = 'B', kind='hexbin', gridsize=25,cmap='Blues', title = 'Hexbin Plots')`

Hexbin plots are similar to scatter plots but plots based on the density of data points on the 2D plane.

Pie Chart

`data['D'].value_counts().plot(kind='pie', title = 'Pie-chart' )`

Plots a pie chart for the specified feature.

On executing the above code blocks one by one, we will get the following images.

### Useful Parameters In Plotting

Now we know how to simply plot or generate graphs just by fitting a dataframe. There are a lot of features or parameters that can be exploited to come up with better looking and more intuitive graphs.Let’s look at some of the most useful ones.

- figsize :This keyword argument allows to set the size of the image that is generated by the plot method.It is passed a tuple containing the width and height of the image (w,h).
- lw : Line width.This parameter allows to set the width of the lines in a line graph
- grid : When the value of grid is set to True, the graph generated will have a grid of lines passing through the axis on the plane.
- title: This parameter allows us to set a title for the graph that is to be generated.
- style : The style keyword argument allows us to set different styles for the lines in a line graph, such as dotted or dashed lines etc.
- marker : marker allows to set a marker symbol for the data points plotted in the generated graph.
- markersize: Allows to set the size for the markers.
- xticks : This keyword argument allows to set the range of markings on the x axis.
- yticks : This keyword argument allows to set the range of markings on the y axis.
- rot : This keyword argument allows to rotate the markings on the x axis for a horizontal plotting and y axis for a vertical plotting.
- fontsize : Allows to set the font size for the labels and axis points.
- colormap : This keyword argument allows to choose different colour sets for the plots.
- alpha : Sets the transparency for the plotted lines, areas, etc.

Let’s have a look at some examples:

`data.plot(kind='line',`

` title="A vs B vs C",`

` figsize=(12,3),`

` lw=3, `

` grid=True, `

` style = '--',`

` marker='s',`

` xticks=[i for i in range(20)], `

` yticks = [i for i in range(-2,3)],`

` rot = 90 ,`

` alpha = 0.5,`

` fontsize = 10)`

Output:

Now let’s add a colour map and a marker.

`data.plot(kind='line',`

` figsize=(12,3), `

` title="A vs B vs C", `

` grid=True, `

` lw = 2, `

` style = '--',`

` xticks=[i for i in range(20)], `

` yticks = [i for i in range(-2,3)],`

` fontsize = 10, `

` colormap = 'gist_rainbow_r',`

` marker='o', `

` markersize=8)`

#### Creating Subplots

Subplots can be created using the keyword argument ‘subplots’ as shown below.

`data.plot(kind='line',`

` title="A vs B vs C",`

` grid=True,`

` style = '--',`

` marker='s',`

` xticks=[i for i in range(20)], `

` yticks = [i for i in range(-2,3)],`

` rot = 90 ,`

` fontsize = 10, `

` colormap = 'flag_r',`

` subplots = True)`

By enabling subplots equal to True all the 3 lines in the above images are plotted independently on different graphs as shown below:

Output:

Take at look at a similar example for area plots:

`data.plot(kind='area',`

` stacked = False, `

` title="A vs B vs C", `

` grid=True, `

` style = ':',`

` xticks=[i for i in range(20)], `

` yticks = [i for i in range(-2,3)],`

` rot = 90 ,`

` fontsize = 10, `

` colormap = 'flag_r',`

` subplots = True)`

` `

Output:

### Graphs With-In Graphs

With a little help from the parent library matplotlib, we can easily create layered plots on a single graph.

`#Importing matplotlib`

`import matplotlib.pyplot as plt`

`#Initializing the figure object`

`fig = plt.figure()`

`#Initializing the axes for the graph`

`axes_1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) `

`axes_2 = fig.add_axes([0.2, 0.5, 0.5, 0.35]) `

The argument that is passed in the above code block is a list containing the position and size of the plots as [left or right, up or down, height, width]

`#Adding labels for axes_1 or Main Graph`

`axes_1.set_xlabel('Number of Observations')`

`axes_1.set_ylabel('Values of A,B & C For Each Observation ')`

`#Adding labels for axes_2 or sub graph (smaller graph)`

`axes_2.set_xlabel('Number of Observations')`

`axes_2.set_ylabel('Values of A,B & C For Each Observation ')`

`#Plotting a line graph on the Main graph`

`data.plot(kind='line',ax= axes_1, subplots=False, figsize=(7,7),`

` title="AvsBvsC", grid=True, legend=True, `

` style="--", xticks=[i for i in range(20)], yticks = [i for i in range(-2,3)], `

` xlim=(0,20), ylim=(-2,9))`

`#Plotting an area graph on the sub graph`

`data.plot(kind='area',stacked = False,ax= axes_2, subplots=False, `

` use_index=True, title=None, grid=True, legend=True, `

` style='--')`

On executing the above code blocks we get a graph thats looks like the below image:

Output:

#### Adding Styles from Matplotlib

The plt.style.use method allows us to import styles from the matplot library. Lets look at an example.

`#Importing matplotlib`

`import matplotlib.pyplot as plt`

`#Calling the style 'dark_background'`

`plt.style.use('dark_background')`

`#Initializing the figure object`

`fig2 = plt.figure()`

`#Initializing the axes for the graph`

`axes_1 = fig2.add_axes([0.1, 0.1, 0.8, 0.8]) `

`axes_2 = fig2.add_axes([0.2, 0.5, 0.5, 0.35]) `

`#Adding labels for axes_1 or Main Graph`

`axes_1.set_xlabel('Number of Observations')`

`axes_1.set_ylabel('Values of A,B & C For Each Observation ')`

`#Adding labels for axes_2 or sub graph (smaller graph)`

`axes_2.set_xlabel('Number of Observations')`

`#Plotting a line graph on the Main graph`

`data.plot(kind='line',ax= axes_1, figsize=(7,7),title="A vs B vs C", grid=True, legend=True,`

` xticks=[i for i in range(20)], yticks = [i for i in range(-2,3)],`

` xlim=(0,20), ylim=(-2,9),fontsize=10)`

`#Plotting an area graph on the sub graph`

`data.plot(kind='area',stacked = False,ax= axes_2, use_index=True, title=None, grid=True, `

` legend=False ,fontsize=15)`

Just by calling the plt.style.use method the above image is transformed in to what’s shown below.

Output:

##### Join Our Telegram Group. Be part of an engaging online community. Join Here.

## Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.A Computer Science Engineer turned Data Scientist who is passionate about AI and all related technologies. Contact: [email protected]