Matplotlib is a fundamental Python library, empowers you to create various visualizations to explore and communicate your data effectively. While basic plots like bar charts and scatter plots are essential, delving into advanced visualizations can unlock deeper insights and enhance your storytelling.
Here are the top 10 advanced plots you can create with Matplotlib!
3D Surface plots are useful for exploring relationships between three variables. With Matplotlib, creating a 3D plot is straightforward, allowing for the visualization of landscapes or surfaces. These plots are particularly useful in fields like meteorology, engineering, and finance, where understanding the interaction between variables is crucial.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Define the figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
# Plot the surface
surf = ax.plot_surface(x, y, z, cmap='viridis')
# Add a color bar which maps values to colors
fig.colorbar(surf, shrink=0.5, aspect=5)
# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
# Show the plot
plt.show()
Heatmaps are a great way to represent data where the colors represent the magnitude of a phenomenon. They are widely used for representing correlation matrices, where they can highlight relationships between variables, or for displaying geographical data density.
import matplotlib.pyplot as plt
import numpy as np
# Generating data
data = np.random.rand(10, 10) # Random data for demonstration
# Creating the heatmap
plt.imshow(data, cmap='hot', interpolation='nearest')
# Customization
plt.colorbar()
plt.title('Heatmap Example')
plt.show()
Also Read: How to Plot Heatmaps in Seaborn?
Contour plots are useful for representing three-dimensional data in two dimensions using contour lines. These lines connect points of equal value. Contour plots are commonly used in meteorology and geography to represent elevation or for visualizing the optimization landscapes in machine learning.
import matplotlib.pyplot as plt
import numpy as np
# Generate coordinate matrices from coordinate vectors
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Define the function z = sin(sqrt(x^2 + y^2))
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create a contour plot
plt.figure(figsize=(8, 6))
contour = plt.contour(X, Y, Z, colors='black')
# Plot contour labels
plt.clabel(contour, inline=True, fontsize=8)
# Adding titles and labels
plt.title('Contour Plot')
plt.xlabel('x axis')
plt.ylabel('y axis')
# Display filled contours
plt.contourf(X, Y, Z, cmap='viridis') # Add a color fill
plt.colorbar() # Show color scale
plt.show()
Stream plots, or flow fields, are ideal for representing fluid flow, air currents, or any other vector field. They give a sense of movement and direction, showing how a vector field flows across a plane, which can be crucial for engineering and environmental studies.
Visualizing time series data is a fundamental aspect of data analysis in finance, meteorology, and engineering. Matplotlib can plot complex time series data, making it easier to identify trends, cycles, and seasonality.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Generate a date range
dates = pd.date_range(start='2021-01-01', end='2021-12-31', freq='D')
# Generate random data to simulate a time series
data = np.random.randn(len(dates)).cumsum()
# Create a DataFrame
df = pd.DataFrame(data, index=dates, columns=['Value'])
# Creating the time series plot
plt.figure(figsize=(10, 6))
plt.plot(df.index, df['Value'], marker='o', linestyle='-', markersize=2)
# Customization
plt.title('Time Series Plot Example')
plt.xlabel('Date')
plt.ylabel('Value')
plt.grid(True)
# Rotate date labels for better readability
plt.xticks(rotation=45)
# Display the plot
plt.show()
Violin plots represent numeric data and combine a box plot with a kernel density plot. They prove particularly useful for comparing data distributions across different categories.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Generate sample data
np.random.seed(10) # For reproducible results
data = np.random.normal(loc=0, scale=1, size=100)
categories = ['A']*50 + ['B']*50
data = np.append(data, np.random.normal(loc=0.5, scale=1.5, size=100))
categories = np.array(categories + ['A']*50 + ['B']*50)
# Create a DataFrame for easy plotting with Seaborn
import pandas as pd
df = pd.DataFrame({'Category': categories, 'Value': data})
# Creating the violin plot
plt.figure(figsize=(8, 6))
sns.violinplot(x='Category', y='Value', data=df)
# Customization
plt.title('Violin Plot Example')
plt.xlabel('Category')
plt.ylabel('Value')
# Display the plot
plt.show()
Visualize the direction and magnitude of a vector field using arrows. This is valuable for understanding phenomena like wind patterns, fluid flow, or magnetic fields.
import numpy as np
import matplotlib.pyplot as plt
# Define the grid of points
x, y = np.meshgrid(np.linspace(-5, 5, 20), np.linspace(-5, 5, 20))
# Define the vector field
u = -y
v = x
# Creating the vector field plot
plt.figure(figsize=(8, 6))
plt.quiver(x, y, u, v, color='r')
# Customization
plt.title('Vector Field Plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.xlim([-5, 5])
plt.ylim([-5, 5])
# Display the plot
plt.show()
Creating multiple subplots in a single figure is an advanced feature of Matplotlib that allows for the comparison of different datasets or different views of the same data. With GridSpec, you can customize the layout of multiple plots within a figure, controlling their placement, size, and aspect ratio.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
# Create figure and GridSpec layout
fig = plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(3, 3, fig) # 3 rows, 3 columns
# Create subplots in the layout
ax1 = fig.add_subplot(gs[0, :]) # Top row, span all columns
ax2 = fig.add_subplot(gs[1, :-1]) # Middle row, span first two columns
ax3 = fig.add_subplot(gs[1:, 2]) # Last two rows, last column
ax4 = fig.add_subplot(gs[2, 0]) # Bottom row, first column
ax5 = fig.add_subplot(gs[2, 1]) # Bottom row, second column
# Generate random data for plots
data1 = np.random.randn(100).cumsum()
data2 = np.random.rand(10)
data3 = np.random.rand(10,10)
data4 = np.random.randn(100)
data5 = np.random.randn(100).cumsum()
# Plot data
ax1.plot(data1, label='Line Plot')
ax1.set_title('Top Span Plot')
ax1.legend()
ax2.bar(np.arange(len(data2)), data2, color='orange', label='Bar Plot')
ax2.set_title('Bar Plot')
ax2.legend()
ax3.imshow(data3, cmap='viridis', aspect='auto', label='Heatmap')
ax3.set_title('Heatmap')
ax4.hist(data4, bins=20, color='green', label='Histogram')
ax4.set_title('Histogram')
ax4.legend()
ax5.scatter(np.arange(len(data5)), data5, color='red', label='Scatter Plot')
ax5.set_title('Scatter Plot')
ax5.legend()
# Adjust layout to make room for titles and labels
plt.tight_layout()
# Show plot
plt.show
Also Read: Tableau for Beginners – Data Visualisation made easy
Though primarily achieved through extensions like GeoPandas, one can use Matplotlib to create choropleth maps shading or patterning areas in proportion to a statistical variable. This functionality proves particularly useful for geographic data visualization, illustrating variations in a measurement across a geographic area.
import plotly.express as px
import pandas as pd
# Sample data: State names and a random value for demonstration
data = {
"State": ["CA", "TX", "NY", "FL", "IL", "PA", "OH", "GA", "NC", "MI"],
"Value": [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
}
df = pd.DataFrame(data)
# Create the choropleth map
fig = px.choropleth(df,
locations='State', # DataFrame column with locations
locationmode="USA-states", # Set to plot as US States
color='Value', # DataFrame column with color values
color_continuous_scale=px.colors.sequential.Plasma, # Color scale
scope="usa", # Focus map on the USA
labels={'Value':'Sample Value'}) # Label for the colorbar
fig.update_layout(title_text = 'USA States Choropleth Map') # Add a title
# Show the plot
fig.show()
Similar to vector field plots, quiver plots represent vectors using arrows, but with additional control over arrow properties like size and color. This allows for customization based on specific data characteristics.
import numpy as np
import matplotlib.pyplot as plt
# Define the grid of points
x, y = np.meshgrid(np.linspace(-2, 2, 20), np.linspace(-2, 2, 20))
# Define the vector components
u = -y
v = x
# Creating the quiver plot
plt.figure(figsize=(6, 6))
plt.quiver(x, y, u, v, color='blue')
# Customization
plt.title('Quiver Plot Example')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.axhline(0, color='black',linewidth=0.5)
plt.axvline(0, color='black',linewidth=0.5)
plt.grid(True)
# Show the plot
plt.show()
Matplotlib’s versatility makes it an invaluable tool for advanced data visualization. By mastering these ten types of plots, you can uncover deeper insights into your data and convey complex ideas more effectively. Remember, the key to successful data visualization lies not just in choosing the right type of plot but also in customizing it to best represent your data’s story.
Want to upgrade your Data Visualization skills? Enroll in our FREE Tableau course today!