In the field of data science and data analytics, the visualization of data is a key part of understanding and conveying information. And Matplotlib, as one of the most commonly used visualization libraries in Python, can help you easily create beautiful charts. Whether it's a line chart, bar chart, or other type of chart, Matplotlib can be implemented with simple code to make the presentation of data intuitive and expressive.
Next, we will learn how to use Matplotlib to generate various graphs with 11 examples of useful Python Matplotlib charts and detailed code explanations, and you will learn how to use Matplotlib to add visual highlights to your data analysis. Whether you're new to data science or an experienced practitioner, these helpful charts will be a great addition to your work.
1.简单折线图(Line Plot)
- Graphs and axes: In Matplotlib, the figure is like a box that holds all the parts of the chart. Axes are like small boxes inside a graphic, surrounded by lines and text that illustrate the meaning of the chart.
- Variable naming: Graphical objects are usually named fig, and axis objects are usually named ax.
- Plot data: Use the ax.plot method to plot data on the axes.
- Pylab Interface: Provides an interface similar to MATLAB to automatically create graphs and axes.
- Multiple polylines: You can draw multiple polylines on the same graph by calling the plot function multiple times.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x));
plt.show()
2.简单的散点图(Scatter Plot)
- What is a Scatter Plot: A scatter plot has some points that are not in order. The points of the line chart are connected together.
- How to make it: You can use plt.plot or ax.plot to make a scatter plot.
- Point style: You can change the appearance of a point using specific code. There are instructions in the documentation on which code to use.
- Cool feature of plt.scatter: you can make every dot unique. They can come in different sizes and colors. You can use datasets to achieve this.
- Transparent Points: You can make points transparent by setting the alpha setting. This feature is especially useful when the dots overlap.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
rng = np.random.RandomState(0)
x = rng.randn(100)
y = rng.randn(100)
colors = rng.rand(100)
sizes = 1000 * rng.rand(100)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.3,
cmap='viridis')
plt.colorbar();
plt.show()
3.错误可视化(Error Visualization)
- Why it matters: In science, accurately handling errors is just as important as the measurement itself.
- For example: in order to estimate the Hubble constant, you have to consider error. This will help you determine if the results are consistent across measurements.
- Uncertainty: When you report a measurement, also state your level of uncertainty (e.g. 71±2.5 (km/s)/Mpc). This allows you to compare data better.
- Display Error: Adding error information to the chart provides more complete and correct information.
- FMT Settings: It controls the appearance of lines and points in the chart, similar to the code for plt.plot.
- Errorbar function: It lets you change the appearance of error bars, for example by making them lighter to make them easier to see.
- Personalization: Use more errorbar settings to achieve horizontal error bars, unilateral error bars, etc. Customize the chart to your needs.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
x = np.linspace(0, 10, 50)
dy = 0.8
y = np.sin(x) + dy * np.random.randn(50)
plt.errorbar(x, y, yerr=dy, fmt='o', color='black',
ecolor='lightgray', elinewidth=3, capsize=0);
plt.show()
4.密度和等高线图(Density and Contour Plots)
- Display 3D data: Display 3D data, such as contour maps or heat maps, in a 2D chart.
- Functions: Draw contour plots with plt.contour, fill contour plots with plt.contourf, and draw pictures with plt.imshow.
- Prepare the data: Use np.meshgrid to convert a 1D array to a 2D grid.
- Draw contour maps: plt.contour requires x, y, and z data for grids and height points.
- Line style: In a black-and-white contour chart, the dashed line represents a negative value and the solid line represents a positive value.
- Color map: Use cmap to set the color of the contour line, for example, use RdGy to display cluster data.
- Color key: Use plt.colorbar to generate a color key to display the range of values corresponding to the color.
- Fill Contour Maps: Use plt.contourf to fill contour maps to make charts with fewer gaps and better visuals.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
plt.contourf(X, Y, Z, 20, cmap='RdGy')
plt.colorbar();
plt.show()
5.直方图、分箱和密度(Histograms, Binning, and Density)
- Basic histogram: Use Matplotlib's hist() function to quickly draw a basic histogram.
- Parameter adjustment: hist() provides a variety of parameters to adjust the calculation and display of the histogram.
- Setting change: The normed parameter is no longer used. Use the density parameter to normalize the data.
- Personalization: Use histtype='stepfilled' and alpha parameters to draw transparent bar charts. This helps to compare different data.
- Check out the documentation: Check out the plt.hist documentation for more information on how to adjust it.
- Get data only: If you only need histogram data and don't need plotting, you can use np.histogram().
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)
kwargs = dict(histtype='stepfilled', alpha=0.3, density=True, bins=40)
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs);
plt.show()
6.自定义图例(Custom Legends)
- Importance: The legend adds meaning to the visualization and clarifies the different elements of the diagram.
- Create a legend: Use plt.legend() to add a name and legend to the chart section.
- Custom: Select the location of the legend. Remove the borders. Use multi-line display. Round corners or add shadows. Change transparency and text spacing.
- Choose what to display: Specify which sections and names appear in the legend. This is achieved by setting what the plot returns.
- Multiple lines: plt.plot can return multiple lines. Select the line you want to pass to plt.legend() to create the legend you need.
- Name the sections: Add names directly to the diagram section and put them in the legend to make it easier to see what each section means.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
x = np.linspace(0, 10, 1000)
y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5))
plt.plot(x, y[:, 0], label='first')
plt.plot(x, y[:, 1], label='second')
plt.plot(x, y[:, 2:])
plt.legend(framealpha=1, frameon=True);
plt.show()
7.自定义颜色条(Custom Colorbars)
- Legend vs. color bar: The legend marks discrete labels, while the color bar represents the relationship between a continuous value and color.
- Create a color bar: Use plt.colorbar() to create a color bar.
- Colormap: Use the cmap parameter to specify a colormap.
- Colormap Resources: All available colormaps are in the plt.cm module, and you can use IPython's Tab to autocomplete the list colormaps.
- Colormap Type:
- Continuous color map: A single continuous series of colors, such as binary or viridis.
- Bifurcation color map: Two contrasting color series, such as RdBu or PuOr.
- Qualitative color map: A discontinuous combination of colors, such as rainbow or jet.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('classic')
x = np.linspace(0, 10, 1000)
I = np.sin(x) * np.cos(x[:, np.newaxis])
plt.imshow(I, aspect='auto', cmap='viridis')
plt.colorbar()
plt.show()
8.多子图(Multiple Subplots)
- Concept: Matplotlib allows multiple subplots to be displayed in a single plot to present the data side by side.
- 创建子图:使用plt.axes()创建子图,可以通过列表参数[left, bottom, width, height]指定位置和大小。
- Default behavior: plt.axes() creates an axis object by default that fills the entire graphics area.
- Alignment: Use plt.subplot() to create a subplot in a grid, you can specify the parameters for the number of rows, the number of columns, and the index of the subplot.
- Multi-row/column: Use plt. GridSpec() creates subgraphs that span multiple rows or columns, defining the grid layout.
- GridSpec Usage: plt.GridSpec() defines the grid layout and can be passed to plt.subplot() to create subgraphs.
- GridSpec Features: It does not directly create charts, but defines the grid layout of subgraphs.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 3000).T
fig = plt.figure(figsize=(6, 6))
grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)
main_ax = fig.add_subplot(grid[:-1, 1:])
y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)
x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)
main_ax.plot(x, y, 'ok', markersize=3, alpha=0.2)
x_hist.hist(x, 40, histtype='stepfilled',
orientation='vertical', color='gray')
x_hist.invert_yaxis()
y_hist.hist(y, 40, histtype='stepfilled',
orientation='horizontal', color='gray')
y_hist.invert_xaxis()
plt.show()
9.文本和注释(Text and Annotations)
- Purpose: The purpose of the visualization is to convey the story behind the data to the reader.
- Expression: Some charts tell a story on their own, while others require textual aid.
- Annotations: Adding text notes to your diagram can grab the reader's attention and aid understanding.
- Text Placement: Use plt.text or ax.text to add text at a specific location (x,y) in the chart.
- Guidance: Draw attention to key diagram features with text annotations to enhance the message.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
plt.style.use('seaborn-v0_8-whitegrid')
# Create sample dataset
dates = pd.date_range(start='1969-01-01', end='1988-12-31', freq='D')
data = np.random.randint(3000, 5000, size=len(dates))
births = pd.DataFrame(data, columns=['births'], index=dates)
fig, ax = plt.subplots(figsize=(12, 4))
births_by_date = births.pivot_table('births', [births.index.month, births.index.day])
births_by_date.index = [pd.Timestamp(year=2012, month=month, day=day) for month, day in births_by_date.index]
births_by_date.plot(ax=ax)
# Annotate specific positions on the line plot
style = dict(size=10, color='gray')
ax.text(pd.Timestamp('2012-1-1'), 3950, "New Year's Day", **style)
ax.text(pd.Timestamp('2012-7-4'), 4250, "Independence Day", ha='center', **style)
ax.text(pd.Timestamp('2012-9-4'), 4850, "Labor Day", ha='center', **style)
ax.text(pd.Timestamp('2012-10-31'), 4600, "Halloween", ha='right', **style)
ax.text(pd.Timestamp('2012-11-25'), 4450, "Thanksgiving", ha='center', **style)
ax.text(pd.Timestamp('2012-12-25'), 3850, "Christmas", ha='right', **style)
# Set title and y-axis label
ax.set(title='USA births by day of year (1969-1988)',
ylabel='average daily births')
# Center month labels on the x-axis
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%b'))
plt.show()
10.自定义刻度(Custom Ticks)
- Modify the scale: Matplotlib's default scale works well for most charts. However, for some charts, you may need to make modifications.
- Components: All chart parts in Matplotlib are things. The Figure contains axes. Axes contain the various contents of the chart.
- Axis content: The axes are xaxis and yaxis. They include lines, scales, names, etc.
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
fig, ax = plt.subplots()
x = np.linspace(0, 3 * np.pi, 1000)
ax.plot(x, np.sin(x), lw=3, label='Sine')
ax.plot(x, np.cos(x), lw=3, label='Cosine')
ax.grid(True)
ax.legend(frameon=False)
ax.axis('equal')
ax.set_xlim(0, 3 * np.pi);
plt.show()
11.创建Matplotlib中的3D图表(Creating 3D Plots in Matplotlib)
- From 2D to 3D: Matplotlib initially only supported 2D charts; The 3D drawing tool was introduced in version 1.0.
- 3D Toolkit: Matplotlib supports 3D plotting by loading the mplot3d toolkit.
- 3D chart type: When the 3D axis is activated, various 3D charts can be drawn, such as line plots and scatter plots.
- 3D绘图函数:使用ax.plot3D和ax.scatter3D分别绘制3D线图和散点图。
- Transparency and Depth: Scatter plots have different transparency to represent depth.
- Interactive view: The interactive view provides a better 3D visual experience.
- 3D Bump Map: mplot3d toolkit has tools to make 3D bump maps. They are similar to ax.contour diagrams in 2D.
- 3D Surface Map: A surface map uses color to fill shapes to show off a 3D surface and looks better than wireframes.
- Grid Data Images: The 3D images generated from the grid data include wireframes and surface diagrams. They present the data on 3D shapes, making it clearer and more convincing.
- Modify the tick style: The normal tick style works in most cases. But for some charts, such as sine and cosine charts, you may need to modify them.
- Custom example: Modifying the position and appearance of the tick can help show what the chart means. You can adjust the scale based on the data to make it more suitable.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# Flatten the X, Y, Z arrays for plot_trisurf
ax.plot_trisurf(X.flatten(), Y.flatten(), Z.flatten(),
cmap='viridis', edgecolor='none')
plt.show()
Here are 11 practical examples that explain how to generate a variety of charts with Matplotlib, including line charts, scatter plots, error visualizations, density charts, histograms, custom legends and color bars, text annotations, custom ticks, and 3D charts. These examples will help you make your data presentation more intuitive and expressive, whether you're a novice or an experienced data science practitioner.