Thanks to theidioms.com

Visualizing data using Pandas

pandas full course
Tutorial

Visualizing data using Pandas

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Pandas uses another popular Python module called Matplotlib for visualization. Usually, visualization is done from data that is stored as pandas objects. This chapter covers a few simple examples showing visualizing data using pandas.


Plot a Line Plot using Pandas

A line plot of data can be made using plot() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd
import matplotlib.pyplot as plt

# Load data into dataframe
data = pd.read_csv("https://github.com/plotly/datasets/raw/master/2014_apple_stock.csv", index_col=0)

# Plot the line from dataframe specifying x and y labels
data.plot()
plt.xlabel('time')
plt.ylabel('value')

Line plot in Pandas

Plot a Scatter Plot using Pandas

A scatter plot of data can be made using plot.scatter() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd

# Loading the data into dataframe
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])

# Making the scatter plot
ax1 = df.plot.scatter(x='length',
                      y='width',
                      c='DarkBlue')
Scatter plot in Pandas

Plot a Histogram Plot using Pandas

A histogram plot of data can be made using plot.hist() function of DataFrame object as shown in the example below:

'''
This histogram below shows the distribution of each value when we:
- draw one dice 9000 times 
- draw two dices 9000 time and sum the result
'''

# Making necessary imports
import pandas as pd
import numpy as np

# Create dataframe
df = pd.DataFrame(
    np.random.randint(1, 7, 9000),
    columns = ['one'])

# Add new column to dataframe by using addition 
df['two'] = df['one'] + np.random.randint(1, 7, 9000)
ax = df.plot.hist(bins=12, alpha=0.8)
Histogram Plot in Pandas

Plot a Bar Graph using Pandas

A bar plot of data can be made using pandas.DataFrame.plot.bar() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd
import numpy as np

# Loading the data into dataframe
df = pd.DataFrame({'Cake_items':['kitkat', 'Unicorn', 'Chocolateroll', 'Barbiedoll','Doraemon'],
                   'Sales':[235,554,582,695,545]})

# Making the bar plot
ax = df.plot.bar(x='Cake_items', y='Sales', rot=0)
Bar Graph in Pandas

Saving plots in Pandas

Pandas plots can be easily saved using savfig() function of matplotlib as shown in the example below:

# plot made using pandas
ax = df.plot()

# getting the figure from plot
fig = ax.get_figure()

# saving plot as png image
fig.savefig('plot.png') # This saves to current directory; you can also specify some other paths

With this, we have come to the end of our Pandas for Data Science Course.

We hope that this course helped you as a stepping stone towards your Data Science journey. Also, if you have any questions or feedback, please feel free to let us know in the comment section. Feel free to browse for other courses on our platform to continue your journey towards Data Science with Python.


Visualizing data using PandasVisualizing data using Pandas

Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in:

  1. Introduction to Python (Free Course) - 1,000,000+ students already enrolled!
  2. Introduction to Data Science  in Python- 400,000+ students already enrolled!
  3. Introduction to TensorFlow for Deep Learning with Python - 90,000+ students already enrolled!
  4. Data Science and Machine Learning Bootcamp with R - 70,000+ students already enrolled!

Leave your thought here

Your email address will not be published. Required fields are marked *