12 Essential Plot Types for Effective Data Visualization

Introduction

In today‘s data-driven world, the ability to effectively visualize and communicate insights from data is a critical skill. Choosing the right type of plot is key to conveying your message and telling a compelling story with your data.

While there are many different plot types to choose from, in this article we‘ll focus on 12 essential plots that every data scientist and analyst should know. For each, we‘ll explain what it is, when you should use it, and show you how to create it using popular Python libraries like Matplotlib, Seaborn and Plotly. Let‘s dive in!

1. Bar Graph

A bar graph uses rectangular bars to compare values across categories. The length of the bar represents the value for that category. Bar graphs are an excellent choice when you want to compare values between different groups, or track changes over time for a given group.

Bar Graph Example

Horizontal bar graphs can be effective when you have long category names. You can easily create a bar graph in Python using Matplotlib‘s bar() function:

import matplotlib.pyplot as plt

categories = [‘A‘, ‘B‘, ‘C‘, ‘D‘]
values = [25, 50, 75, 100]

plt.figure(figsize=(10, 5))
plt.bar(categories, values)
plt.show()

2. Line Graph

A line graph displays a series of data points connected by a line. It is commonly used to visualize a trend in data over a continuous time interval. Use line graphs when you want to show how a variable changes over time.

Line Graph Example

Creating a basic line graph is simple with Matplotlib‘s plot() function:

import matplotlib.pyplot as plt

years = [2015, 2016, 2017, 2018, 2019]
values = [100, 400, 900, 1600, 2500] 

plt.figure(figsize=(10, 5))
plt.plot(years, values)
plt.show()

3. Pie Chart

A pie chart displays values as slices of a circle, with the size of each slice representing the proportion of the total. While overused and often criticized, a pie chart can be effective for showing the composition of a whole.

Pie Chart Example

Use Matplotlib‘s pie() function to easily bake up a pie chart:

import matplotlib.pyplot as plt

labels = [‘A‘, ‘B‘, ‘C‘]
sizes = [30, 50, 20]

plt.figure(figsize=(5, 5))
plt.pie(sizes, labels=labels, autopct=‘%1.1f%%‘)
plt.axis(‘equal‘)  
plt.show()

4. Histogram

A histogram plots the distribution of a numeric variable, with the area of each bar representing the frequency of values falling within that range. Use a histogram to understand the shape of your data‘s distribution.

Histogram Example

Matplotlib‘s hist() function makes it a snap to create a histogram:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.normal(0, 1, 1000)

plt.figure(figsize=(10, 5))
plt.hist(data, bins=20)
plt.show()  

5. Scatter Plot

A scatter plot displays values for two numeric variables as points in 2D space. Use a scatter plot to visualize the relationship between two variables, or to identify clusters and outliers in your data.

Scatter Plot Example

With Seaborn‘s scatterplot() function, creating an informative scatter plot requires minimal code:

import seaborn as sns

penguins = sns.load_dataset("penguins")

sns.scatterplot(data=penguins, x="body_mass_g", y="bill_length_mm", hue="species")

6. Box Plot

A box plot, or box-and-whisker plot, displays the distribution of a numeric variable through its quartiles. The box represents the interquartile range (IQR), with the median marked inside. The whiskers extend to the minimum and maximum values, excluding outliers which are plotted as individual points. Use box plots to compare distributions between groups.

Box Plot Example

Seaborn‘s boxplot() function allows you to quickly visualize the distribution of a variable across categories:

import seaborn as sns

tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", data=tips)

7. Heatmap

A heatmap is a 2D representation of data where values are encoded as colors. Heatmaps are commonly used to visualize the relationship between two variables, or to understand patterns in a matrix or grid format.

Heatmap Example

With Seaborn, creating an attractive and insightful heatmap is just a few lines of code:

import seaborn as sns

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")

sns.heatmap(flights, cmap="YlGnBu")

8. Violin Plot

Violin plots are similar to box plots, but display the full distribution of the data using kernel density estimation. The wider regions represent higher density of data points. Use violin plots when you want to visualize the distribution of a variable across multiple categories.

Violin Plot Example

Seaborn‘s violinplot() function creates beautiful violin plots with ease:

import seaborn as sns

tips = sns.load_dataset("tips")

sns.violinplot(x="day", y="total_bill", data=tips)  

9. Bubble Chart

A bubble chart is a variation of a scatter plot where a third dimension of the data is represented by the size of the points. Use bubble charts to visualize the relationship between three numeric variables.

Bubble Chart Example

You can create an impressive interactive bubble chart with Plotly Express:

import plotly.express as px

gapminder = px.data.gapminder()

fig = px.scatter(gapminder, x="gdpPercap", y="lifeExp", size="pop", color="continent",
                 hover_name="country", log_x=True, size_max=60,
                 animation_frame="year", range_x=[100,100000], range_y=[25,90])
fig.show()

10. Radar Chart

Also known as a spider or star chart, a radar chart plots multiple quantitative variables on axes starting from the same point. Use radar charts to compare multiple variables or profiles in a visually compelling way.

Radar Chart Example

While not natively supported, you can create a radar chart using Matplotlib‘s polar projection:

import matplotlib.pyplot as plt
import numpy as np

categories = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘]
values = [38, 29, 19, 16, 23]

angles = np.linspace(0, 2 * np.pi, len(categories), endpoint=False)
values = np.concatenate((values, [values[0]])) 
angles = np.concatenate((angles, [angles[0]]))

fig = plt.figure(figsize = (6, 6))
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, values, ‘o-‘, linewidth=2)
ax.fill(angles, values, alpha=0.25)
ax.set_thetagrids(angles * 180/np.pi, categories)
plt.show()

11. Treemap

A treemap displays hierarchical data using nested rectangles. The size of each rectangle corresponds to a quantitative dimension of the data. Treemaps are useful for visualizing large hierarchical datasets in a compact space.

Treemap Example

The squarify library in Python makes it easy to create treemaps:

import squarify 
import matplotlib.pyplot as plt

labels = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘]
sizes = [15, 20, 5, 25, 15, 20]
colors = [‘red‘, ‘green‘, ‘blue‘, ‘orange‘, ‘purple‘, ‘yellow‘]

plt.figure(figsize=(10, 6))
squarify.plot(sizes=sizes, label=labels, color=colors, alpha=0.7)
plt.axis(‘off‘)
plt.show()

12. Geographic Map

Geographic maps are essential for visualizing geospatial data. Depending on your data and goals, you might use a choropleth map, bubble map, or heatmap overlaid onto a map projection.

Geographic Map Example

The Plotly Express library provides high-level functions for creating interactive geographic visualizations:

import plotly.express as px

df = px.data.election()
geojson = px.data.election_geojson()

fig = px.choropleth_mapbox(df, geojson=geojson, color="Bergeron",
                           locations="district", featureidkey="properties.district",
                           center={"lat": 45.5517, "lon": -73.7073}, zoom=9,
                           mapbox_style="carto-positron", range_color=[0, 6500])
fig.show()

Cheat Sheet

Here‘s a handy cheat sheet summarizing the plot types we‘ve covered and the key functions to create them in Python:

  • Bar Graph: plt.bar()
  • Line Graph: plt.plot()
  • Pie Chart: plt.pie()
  • Histogram: plt.hist()
  • Scatter Plot: sns.scatterplot()
  • Box Plot: sns.boxplot()
  • Heatmap: sns.heatmap()
  • Violin Plot: sns.violinplot()
  • Bubble Chart: px.scatter()
  • Radar Chart: plt.figure(subplot_kw={‘projection‘: ‘polar‘})
  • Treemap: squarify.plot()
  • Geographic Map: px.choropleth_mapbox()

Conclusion

Effective data visualization is both an art and a science. By understanding the strengths and use cases of essential plot types like the ones we‘ve covered here, you‘ll be well equipped to explore and communicate insights from a wide variety of datasets.

As you continue on your data visualization journey, remember that great graphs are designed with the audience in mind. Always strive for clarity, choose the right plot for the task at hand, and don‘t be afraid to iterate and refine your approach.

With the power of Python and its vast ecosystem of visualization libraries, you have everything you need to create stunning, insightful graphics. The only limit is your creativity and commitment to the craft of data storytelling. Happy visualizing!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts