Thanks to theidioms.com

Visualizing data using Pandas

pandas full course
Tutorial

Visualizing data using Pandas

Visualizing data using PandasVisualizing data using Pandas

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.


Are you interested in working with data? Get a free trial month of LinkedIn Learning and learn Data Science and Machine Learning courses using Python, R, and SQL from top instructors.

Enroll in any one of the courses from the following fields today on LinkedIn Learning and start your free month of learning:

Disclaimer: When you subscribe to LinkedIn Learning, we may earn a small commission as an affiliate. We love the platform so much so that we have been using it ourselves at The Click Reader.

Visualizing data using PandasVisualizing data using Pandas

Leave your thought here

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