Data visualization is one of the most important aspects of data analysis, as it helps to display the insights hidden in the data in a more meaningful and understandable way. In this blog post, we will explore the top 10 Python libraries for data visualization that can help you create stunning visualizations and make better decisions.
Python is a high-level programming language that has gained immense popularity over the years for its simplicity, power, and versatility. Python libraries play a crucial role in making programmers’ lives much easier by providing specific functions that can be utilized in their programs.
Matplotlib:
Matplotlib is a plotting library that is widely used for creating 2D plots. It provides an object-oriented API for embedding plots into applications and supports various output formats like PDF, SVG, and PNG. Let’s take a look at a simple example:
import matplotlib.pyplot as plt import numpy as np x = np.arange(0, 10, 0.1) y = np.sin(x) plt.plot(x, y) plt.show()
Seaborn:
Seaborn is a library built on top of Matplotlib that provides a high-level interface for creating beautiful statistical graphics. It has various built-in color palettes and themes that enhance the visual appeal of the graphs. Here’s an example:
import seaborn as sns import pandas as pd iris = sns.load_dataset('iris') sns.pairplot(iris, hue='species')
Plotly:
Plotly is an open-source visualization library that is popular for creating interactive plots. It supports various types of plots like scatter plots, line charts, bar plots, and maps. Here’s a simple example:
import plotly.express as px import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv') fig = px.scatter(df, x='gdpPercap', y='lifeExp', color='continent', size='pop', size_max=60, log_x=True, range_x=[100, 100000]) fig.show()
Bokeh:
Bokeh is a visualization library that supports creating interactive and browser-based plots. It provides a high-level interface for creating various types of plots, including line charts, scatter plots, and heatmaps. Here’s an example:
from bokeh.plotting import figure, output_file, show import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv') p = figure(title='Life Expectancy vs. GDP', x_axis_label='GDP per Capita', y_axis_label='Life Expectancy') p.circle(df['gdpPercap'], df['lifeExp'], color='blue', size=10) show(p)Altair:
Altair is a declarative visualization library built on top of Vega-Lite, which is a grammar of interactive graphics. It follows the philosophy of a grammar of graphics approach, which makes it easy to create complex visualizations with minimal code. Here's an example:import altair as alt import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/vega/vega-datasets/master/data/cars.csv') chart = alt.Chart(df).mark_circle().encode( x='Horsepower', y='Miles_per_Gallon', color='Origin', tooltip=['Name', 'Horsepower', 'Miles_per_Gallon'] ).interactive() chart.show()
Plotnine:
Plotnine is a visualization library based on the Grammar of Graphics philosophy, which makes it easy to create complex visualizations with minimal code. It is designed to work with ggplot2, a popular data visualization library for R. Here's an example:from plotnine import ggplot, aes, geom_point import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris.csv') plot = ggplot(df, aes(x='Petal.Length', y='Petal.Width', color='Species')) + geom_point() plot.draw()Holoviews:
Holoviews is a library that allows users to easily create interactive visualizations from a wide range of data sources. It supports a wide range of plot types, including scatter plots, line charts, and heatmaps. Here's an example:
import holoviews as hv import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv') scatter = hv.Scatter(df, ('gdpPercap', 'GDP per Capita'), ('lifeExp', 'Life Expectancy'), 'continent') scatter.opts(color='continent', tools=['hover'])Ggplot:
Ggplot is a port of ggplot2, which is a popular data visualization library for R. Ggplot provides a way to create complex visualizations with minimal code and follows the Grammar of Graphics philosophy. Here's an example:from ggplot import * df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris.csv') chart = ggplot(df, aes(x='Petal.Length', y='Petal.Width', color='Species')) + \ geom_point() + \ labs(title='Petal Width vs. Petal Length', x='Petal Length', y='Petal Width') chartChord:
Chord is a library that creates interactive visualizations of data using Chord diagrams. It is useful when you want to show the relationships between different entities. Here's an example:
from chord import Chord import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv') matrix = pd.crosstab(df.Origin, df.Dest) Chord(matrix)Data visualization is an essential aspect of data analysis, and Python libraries make it easier to create meaningful and beautiful visualizations. The libraries mentioned in this article are just a few of the many available ones, but they can be a great starting point for creating stunning visualizations that can help you make better decisions based on your data.
Want to learn more about Python, checkout the Python Official Documentation for detail.
You may also like our Python Learning path under following Categories: