Pandas - Visualizing Dataframe Data - 7 Days of Pandas
Tags: beginners, python
Published: 2022-12-26 10:09:11

Welcome to the sixth article in the "7 Days of Pandas" series where we cover the `pandas` library in Python which is used for data manipulation.

In the [first article](https://dev.to/piyushraj/pandas-read-and-write-data-from-csv-files-7-days-of-pandas-1o4f) of the series, we looked at how to read and write CSV files with Pandas. In this tutorial, we will look at some of the most common operations that we perform on a dataframe in Pandas.  

In the [second article](https://dev.to/piyushraj/pandas-basic-data-manipulation-7-days-of-pandas-4c47), we looked at how to perform basic data manipulation.   

In the [third article](https://dev.to/piyushraj/pandas-basic-exploratory-data-analysis-7-days-of-pandas-3816), we looked at how to perform EDA (exploratory data analysis) with Pandas.  

In the [fourth article](https://dev.to/piyushraj/pandas-handling-missing-values-7-days-of-pandas-a16), we looked at how to handle missing values in a dataframe.

In the [fifth article](https://dev.to/piyushraj/pandas-aggregating-and-grouping-data-7-days-of-pandas-p60) we looked at how to aggregate and group data in Pandas

In this tutorial, we will look at how to plot data in a pandas dataframe with the help of some examples.

Data visualizations are a great way to present data and can help us find insights that may not have been obvious with the data in just tabular form. For example, if you have the data of salaries of employees in an office, a bar chart would give you a much more intuitive feel for comparing them.

## How to visualize data in pandas dataframes?
You can use the pandas dataframe [`plot()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html) to create a plot from the dataframe values. It creates a matplotlib plot. You can specify the x and y values of the plot with `x` and `y` parameters respectively and the type of plot you want to create with the `kind` parameter.

Let's look at some common types of plots that you can create from pandas dataframe data.

Before we begin, let's first import pandas and create a sample dataframe that we will be using throughout this tutorial.


```python
import pandas as pd

# employee data
data = {
    "Name": ["Tim", "Shaym", "Noor", "Esha", "Sam", "James", "Lily"],
    "Gender": ["M", "M", "F", "F", "M", "M", "F"],
    "Age": [26, 28, 27, 32, 24, 31, 33],
    "Department": ["Marketing", "Product", "Product", "HR", "Product", "HR", "Marketing"],
    "Salary": [60000, 70000, 82000, 55000, 58000, 55000, 65000]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Name</th>
      <th>Gender</th>
      <th>Age</th>
      <th>Department</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>Tim</td>
      <td>M</td>
      <td>26</td>
      <td>Marketing</td>
      <td>60000</td>
    </tr>
    <tr>
      <th>1</th>
      <td>Shaym</td>
      <td>M</td>
      <td>28</td>
      <td>Product</td>
      <td>70000</td>
    </tr>
    <tr>
      <th>2</th>
      <td>Noor</td>
      <td>F</td>
      <td>27</td>
      <td>Product</td>
      <td>82000</td>
    </tr>
    <tr>
      <th>3</th>
      <td>Esha</td>
      <td>F</td>
      <td>32</td>
      <td>HR</td>
      <td>55000</td>
    </tr>
    <tr>
      <th>4</th>
      <td>Sam</td>
      <td>M</td>
      <td>24</td>
      <td>Product</td>
      <td>58000</td>
    </tr>
    <tr>
      <th>5</th>
      <td>James</td>
      <td>M</td>
      <td>31</td>
      <td>HR</td>
      <td>55000</td>
    </tr>
    <tr>
      <th>6</th>
      <td>Lily</td>
      <td>F</td>
      <td>33</td>
      <td>Marketing</td>
      <td>65000</td>
    </tr>
  </tbody>
</table>
</div>



## Scatter Plot

To create a [scatter plot with dataframe data](https://datascienceparichay.com/article/scatter-plot-from-pandas-dataframe/), pass "scatter" to the `kind` parameter of the `plot()` function. For example, let's create a scatter plot of the "Age" vs "Salary" data in the above dataframe.


```python
df.plot(x="Age", y="Salary", kind="scatter")
```


![scatter plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6p6osp8ijwf53tm6nvm.png)



You can also customize the plot with additional parameters to the `plot()` function. For example, let's add a title to the plot and change the color of the points.


```python
df.plot(x="Age", y="Salary", kind="scatter", title="Salary v/s Age", color='red')
```

![scatter plot with title and red scatter points](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7teztpg0c8b47bklez12.png)


## Bar Plot

To create a bar plot, pass "bar" as an argument to the `kind` parameter. Let's create a bar plot of the "Salary" column in the above dataframe.


```python
df.plot(y="Salary", x="Name", kind="bar")
```


![bar plot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v2vwslhktil7ajs1lbf.png)

    


You can also customize the plot with additional parameters to the `plot()` function. For example, let's rotate the xtick labels slightly and change the color of the bars.


```python
df.plot(y="Salary", x="Name", kind="bar", rot=30, color="teal")
```


![bar plot formatted with teal colored bars and rotated xtick labels](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idxwh5xekx751jmis7d6.png)

    


## Histogram

A histogram is used to look at the distribution of a continuous variable. To plot a histogram on pandas dataframe data, pass "hist" to the `kind` parameter.

For example, let's plot a histogram of the values in the "Age" column.


```python
df.plot(y="Age", kind="hist", bins=3)
```


![histogram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qly0w4l68dycew0o5t4n.png)

    


You can also directly apply the `plot()` function to a pandas series.


```python
df['Age'].plot(kind="hist", bins=3)
```


![histogram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klbvuwu0xsubaatulxln.png)

    


We get the same result.

You can similarly plot other types of plots (for example, [line plot](https://datascienceparichay.com/article/plot-pandas-series-as-a-line-plot/), [pie chart](https://datascienceparichay.com/article/create-a-pie-chart-of-pandas-series-values/), etc.) with the `plot()` function using the appropriate parameters.