Data Visualization: How to choose the right chart [Part 1] | by XuanKhanh Nguyen | Towards Data Science

According to the World Economic Forum, the world produces 2.5 quintillion bytes of data every day. With so much data, it’s become increasingly difficult to manage and make sense of it all. It would be impossible for any person to wade through data line-by-line and see distinct patterns and make observations.
Data visualization is one of the data science processes; that is, a framework for approaching data science tasks. After data is collected, processed, and modeled, the relationships need to be visualized for the conclusions.
We use data visualization as a technique to communicate insights from data through visual representation. Our main goal is to distill large datasets into visual graphics to allow for a straightforward understanding of complex relationships within the data.
So now, we know data visualization can provide insight that traditional descriptive statistics cannot. Our big question is how to choose the right chart for the data?
This note will give us an overview of the different chart types. For each type of chart, we will introduce a short description. We then discuss when to use it and when we should avoid using it. Next, we will look at some Python code for implementation. I only present the primary principle; the full version will be provided at the end of this article.
I hope this note is interesting enough to pick up the slack. Let’s hop to it.
Before making a chart, it’s essential to understand why we need one. Graphs, plots, maps, and diagrams help people understand complex data, find patterns, identify trends, and tell stories. Think about the message we want to share with our audience. Here, I group the charts by their data visualization functions, that is, what we want our charts to communicate with our audience. While each chart’s allocation into specific functions isn’t a perfect system, it still works as a useful guide for selecting a chart based on our analysis or communication needs.
The first part of this note will introduce us to different charts to display the connection between variables, the trend over time, and the relative order of variables within category(ies)
1. Scatter plot using Matplotlib2. Marginal Histogram3. Scatter plot using Seaborn4. Pair Plot in Seaborn5. Heat Map
6. Line Chart7. Area Chart8. Stack Area Chart9. Area Chart Unstacked
10. Vertical Bar Chart11. Horizontal Bar Chart12. Multi-set Bar Chart13. Stack Bar Chart14. Lollipop Chart
The second part of this note will introduce us to different chart types use to compare variables and their distribution.
15. Histogram16. Density Curve with Histogram17. Density Plot18. Box Plot19. Strip Plot20. Violin Plot21. Population Pyramid
22. Bubble Chart23. Bullet Chart24. Pie Chart25. Net Pie Chart26. Donut Chart27. TreeMap28. Diverging Bar29. Choropleth Map30. Bubble Map
We use a relationship method to display a connection or correlation between two or more variables.
When assessing a relationship between data sets, we are trying to understand how two or more data sets combine and interact with each other.
This relationship is called correlation, and it can be positive or negative, meaning that the variables considered might be supportive or working against each other.
A scatter plot is a type of chart that is often used in statistics and data science. It consists of multiple data points plotted across two axes. Each variable depicted in a scatter plot would have various observations. It can be an advantageous chart type whenever we see any relationship between the two data sets.
We use a scatter plot to identify the data’s relationship with each variable (i.e., correlation or trend patterns.) It also helps in detecting outliers in the plot.
In machine learning, scatter plots are often used in regression, where x and y are continuous variables. They are also used in clustering scatters or outlier detection.
Scatter plots are not suitable if we are interested in observing time patterns.
A scatter plot is used with numerical data or numbers. So, if we have categories such as three divisions, five products, etc., a scatter plot would not reveal much.
We use the Iris dataset for visualization.
plt.scatter(iris_df['sepal length (cm)'], iris_df['sepal width (cm)'])plt.title('Scatterplot of Distribution of Sepal Length and Sepal Width', fontsize=15)plt.xlabel('sepal length (cm)')plt.ylabel('sepal width (cm)')
Marginal histograms are histograms added to the margin of each axis of a scatter plot for analyzing the distribution of each measure.
We use a marginal histogram to assess the relationship between two variables and examine their distributions. Putting marginal histograms in scatter plots or adding marginal bars on highlighted tables makes the visualization interactive, informative, and impressive.
# A seaborn jointplot shows bivariate scatterplots and univariate histograms in the same figurep = sns.jointplot(iris_df['sepal length (cm)'], iris_df['sepal width (cm)'], height=10)
Our goal here is to produce a legend to understand the differences between groups. We will use seaborn’s FacetGrid to color the scatterplot by species.
sns.FacetGrid(iris_df, hue=’species’, size=10) \ .map(plt.scatter, ‘sepal length (cm)’, ‘sepal width (cm)’) \ .add_legend()plt.title(‘Scatterplot with Seaborn’, fontsize=15)
Another useful seaborn plot is pairplot, which shows the bivariate relationship between each pair of features. From the pair plot, we’ll see that the Iris-setosa species is separated from the other two across all feature combinations.
sns.pairplot(iris_df.drop(“target”, axis=1), hue=”species”, height=3)
A heatmap is a graphical representation of data that uses a system of color-coding to represent different values. Heatmaps are useful for cross-examining multivariate data, through placing variables in rows and columns and coloring cells within the table.
All the rows are one category (labels displayed on the left side), and all the columns are another category (labels displayed on the bottom). The individual rows and columns are divided into the subcategories, which all match each other in a matrix. The cells within the table either contain color-coded categorical data or numerical data based on a color scale. Data in a cell demonstrates the relationship between two variables in the connecting row and column.
Heatmaps are useful for showing variance across multiple variables, revealing any patterns, displaying whether any variables are similar, and detecting any correlations between them.
Heatmap can be super useful when we want to see which intersections of the categorical values have a higher concentration of the data than others.
Heatmaps are better suited to displaying a more generalized view of numerical data. It is harder to accurately tell the differences between color shades and extract specific data points (unless we include the cells’ raw data).
Heatmaps can also show the changes in data over time if one of the rows or columns is set to time intervals. An example of this would be to use a heatmap to compare the temperature changes across the year in the city(ies), to see the hottest or coldest places. So the rows contain each month, the columns indicate hours, and the cells would have the temperature values.
We use the World Happiness Report dataset from Kaggle. I cleaned the data and combined all files into the happiness_rank.csv file. You can download and clean the data or download the final result here. I recommend you check out my data cleaning codes on Github.
sns.heatmap(happy[usecols].corr(),linewidths=0.25,            vmax=0.7,square=True,cmap="Blues",             linecolor='w',annot=True,annot_kws={"size":8},              mask=mask, cbar_kws={"shrink": .9})
Sometimes it isn’t enough to know that a relationship exists between variables; in some cases, better analysis is possible if we can also visualize when the relationship took place. Because relationships are denoted with links between variables, the date/time appears as a link property. This visualization method shows data over the period to find trends or changes over time.
Line charts are used to display quantitative values over a continuous interval or period.
Line charts are drawn by first plotting data points on a cartesian coordinate grid and then connecting them. Typically, the y-axis has a quantitative value, while the x-axis is a timescale or a sequence of intervals. The direction of the lines on the graph works as an excellent metaphor for the data: an upward slope indicates increasing values, and a downward slope indicates where values have decreased. The line’s journey across the graph can create patterns that reveal trends in a dataset.
Line charts are most frequently used to show trends and analyze how the data has changed over time.
Line charts are best for continuous data as it connects many variables that all belong to the same category.
When grouped with other lines or other data series, individual lines can be compared. However, we should avoid using more than four lines per graph, as this makes the chart more cluttered and harder to read. A solution to this is to split our chart into multiples subplots.
Suppose we have a dataset containing information about Medium members. We want to see the trend of articles that have been read in 2019.
plt.plot(data['Month'], data['All Views'], color='#4870a0', marker='o')
The idea of an area chart is based on the line chart. The colored region shows us the development of a variable over time.
Area charts are ideal for clearly illustrating the magnitude of change between two or more data points. For example, the happiness score has six generating divisions; we would like to see each of these divisions’ contributions.
Moreover, if we are interested in the portion generated by each division and not that much of the total amount of the division self, we can use a 100% stacked area chart. This will show each division’s percentage contribution over time.
Area charts are not the best choice if we want to present fluctuating values, like the stock market or price changes.
Here, we want to present an accumulative number of external views over time.
plt.stackplot(data['Month'], data['External Views'], colors='#7289da', alpha=0.8)
The idea of a stack area chart is based on the simple area charts. It displays the value of several groups on the same graphic. Values of each group are displayed on top of each other. The entire graph represents the total of all data plotted over time.
The stacked area chart type is a powerful chart as it allows grouping of data and seeing trends over a selected date range.
Stacked area charts use the areas to convey whole numbers, so they do not work for negative values.
Stacked area charts are colorful and fun, but we should use them with caution because they can quickly become a mess. We shouldn’t stack together more than five categories.
plt.stackplot(data['Month'], data['Internal Views'], data['External Views'],              alpha=0.75,              colors=['#7289da','#f29fa9'],            labels=['Internal Views', 'External Views'])
Unlike a stack area chart, an area chart unstacked shows the overlap of several groups on the same graphic.
x = data['Internal Views']y = data['External Views']# plot the dataax.plot(x, color='#49a7c3', alpha=0.3, label='Internal Views')ax.plot(y, color='#f04747', alpha=0.3, label='External Views')# fill the areas between the plots and the x axis# this can create overlapping areas between linesax.fill_between(x.index, 0, x, color='blue', alpha=0.2)ax.fill_between(x.index, 0, y, color='red', alpha=0.2)
A visualization method displays the relative order of data values.
Bar charts are among the most frequently used chart types. As the name suggests, a bar chart is composed of a series of bars illustrating a variable’s development.
There are four types of bar charts: horizontal bar chart, verticle bar chart, group bar chart, and stacked bar chart.
Bar charts are great when we want to track the development of one or two variables over time. One axis of the chart shows the specific categories being compared, and the other axis represents a measured value.
A simple bar chart isn’t suitable when we have a single period breakdown of a variable. For example, if I want to portray the main business lines that contributed to a company’s revenues, I wouldn’t use a bar chart. Instead, I would create a pie chart or one of its variations.
Vertical bar charts (column chart) are distinguished from histograms, as they do not display continuous developments over an interval. Vertical bar chart’s discrete data is categorical and therefore answers the question of “how many?” in each category.
Vertical bar charts are typically used to compare several items in a specific range of values. So it is ideal for comparing a single category of data between individual sub-items, for example, corresponding revenue between regions.
We use mpg_ggplot2 data frame. It is a rectangular collection of variables (in the columns) and observations (in the rows). mpg contains observations collected by the US Environmental Protection Agency on 38 popular models of car.
Here, we want to compare car models.
plt.bar(value_count.index, value_count.values, color='#49a7c3')
Horizontal bar charts represent the data horizontally. The data categories are shown on the y-axis, and the data values are shown on the x-axis. The length of each bar is equal to the value corresponding to the data category, and all bars go across from left to right.
plt.barh(value_count.index, value_count.values, color='#b28eb2')
Also known as a Grouped Bar Chart or Clustered Bar Chart.
This variation of a bar chart is used when two or more data series are plotted side-by-side and grouped under categories, all on the same axis.
We use multi-set bar charts to compare grouped variables or categories to other groups with those same variables or category types.
The downside of group bar charts is that they become harder to read the more bars we have in one group.
ax = views.plot.bar(rot=0,color='#E6E9ED',width=1, figsize=(14,8))ax = df.plot.bar(rot=0, ax=ax, color=['#7289da', '#dd546e', '#99aab5', '#f3c366'],                  width=0.8, figsize=(14,8))
Unlike a multi-set bar chart that displays their bars side-by-side, stacked bar charts segment their bars. Stacked bar charts are used to show how a broader category is divided into smaller categories and what the relationship of each part has on the total amount.
Stacked bar charts place each value for the segment after the previous one. The total value of the bar is all the segment values added together. It is ideal for comparing the total amounts across each group/segmented bar.
One major flaw of Stacked bar charts is that they become harder to read the more segments each bar has. Also, comparing each component to each other is difficult, as they are not aligned on a common baseline.
rect1 = plt.bar(data['Month'] ,data['Internal Views'],                width=0.5, color='lightblue')rect2 = plt.bar(data['Month'], data['External Views'],                width=0.5, color='#1f77b4')
Lollipop chart serves a similar purpose as an ordered bar chart in a visually pleasing way. We use lollipop charts to show the relationship between a numerical variable and another numerical or categorical variable.
The lollipop chart is often claimed to be useful compared to a standard bar chart if we are dealing with a large number of values and when values are all high, such as in the 80–90% range (out of 100%). Then a broad set of tall columns can be visually aggressive.
If our data has unsorted bars of very similar length — it is harder to compare the sizes of two very identical lollipops than standard bars.
Python Implementation
(markerline, stemlines, baseline) = plt.stem(value_count.index, value_count.values)
That’s it for the first part. The code is available on Github. We will continue with distributions and comparisons on part two.
So far, we know that data visualization is a quick, easy way to convey concepts universally — and we can experiment with different scenarios by making slight adjustments.
There are dozens of tools for data visualization and data analysis — these range from simple — zero codings required (Tableau) to complex — coding required (JaveScript). Not every tool is right for every person looking to learn visualization techniques, and not every tool can scale to industry or enterprise purposes.
My favorite professor told me that “Good data visualization theory and skills will transcend specific tools and products.” When we learn this skill, focus on best practices, and explore our style when it comes to visualizations and dashboards. Data visualization isn’t going away anytime soon, so it’s essential to build a foundation of analysis and storytelling, and exploration that you can carry with regardless of the tools or software you end up using.
If you want to dig deeper into this particular topic, here are some excellent places to start.
Information is BeautifulVisualizing dataData Visualization CatalogueColor HexMatplotlib Cheat SheetHow to make a heatmap with Seaborn in Python?
Information is Beautiful
Visualizing data
Data Visualization Catalogue
Color Hex
Matplotlib Cheat Sheet
How to make a heatmap with Seaborn in Python?