Getting Started with Python Data Visualization: Matplotlib Beginner's Guide

Matplotlib is one of the most commonly used data visualization libraries in Python. It provides rich chart types and flexible customization options, which can help us understand data in a more intuitive way. This article will introduce the basic functions of Matplotlib, including how to create and customize charts, etc.

1. Introduction to Matplotlib

Matplotlib is a Python library dedicated to creating 2D charts (including 3D charts). Its name comes from "MATLAB-style plotting", which means that its design goal is to provide Python with similar drawing functions as MATLAB.

The main module of Matplotlib is pyplotthat it provides a set of command-style function sets similar to MATLAB for creating and displaying charts.

Here is a simple example showing how to create a line plot using Matplotlib:

python
复制代码
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

In this example, we first generate a set of x and y values, then create plt.plot(x, y)a line graph with , and finally plt.show()display the graph with .

2. Create different types of charts

Matplotlib supports the creation of many types of charts, including line graphs, scatter plots, histograms, histograms, pie charts, and more.

1. Line graph

A line graph is the most basic type of graph, which shows the changing trend of data by connecting data points in sequence. We have seen how to create a line graph in the previous examples.

2. Scatterplot

A scatterplot is a type of chart used to show the relationship between two variables. We can plt.scatter()create a scatterplot using the function:

python
复制代码
x = np.random.rand(100)
y = np.random.rand(100)

plt.scatter(x, y)
plt.show()

3. Histogram

A histogram is a type of chart used to compare differences between categories. We can plt.bar()create a histogram using the function:

python
复制代码
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

plt.bar(categories, values)
plt.show()

3. Customize the chart

Matplotlib provides many options for customizing charts, for example, we can add titles, labels, change colors, line types, line widths, etc.

python
复制代码
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y, color='red', linestyle='--', linewidth=2)
plt.title('Sin Wave')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.show()

In this example, we changed the color, style and width of the lines, added titles and axis labels, and turned on the grid.

In addition to the above-mentioned basic customization options, Matplotlib also provides more advanced features, such as:

1. Create a subgraph

We can create multiple subcharts in one chart, and each subchart can have different types and settings. We can plt.subplot()create subplots using the function:

python
复制代码
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

plt.subplot(2, 1, 1)  # 创建一个2行1列的子图,并选择第1个子图
plt.plot(x, y1, color='red')
plt.title('Sin Wave')

plt.subplot(2, 1, 2)  # 创建一个2行1列的子图,并选择第2个子图
plt.plot(x, y2, color='blue')
plt.title('Cos Wave')

plt.tight_layout()  # 自动调整子图间距
plt.show()

2. Save the graph

We can plt.savefig()save the chart as an image file using the function:

python
复制代码
x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.savefig('sin_wave.png')

In this example, we save the chart as an image in PNG format. plt.savefig()The function supports multiple image formats, including PNG, JPG, PDF, etc.

4. Conclusion

Matplotlib is a powerful and flexible Python data visualization library, whether it is to quickly create charts or customize complex charts, it can meet the needs well. I hope this article can help you master the basic usage of Matplotlib and provide support for your data analysis and visualization work.

If you are interested in Python and want to get a higher salary by learning Python, then the following set of Python learning materials must be useful to you!

Materials include: Python installation package + activation code, Python web development, Python crawler, Python data analysis, artificial intelligence, machine learning and other learning tutorials. Even beginners with 0 basics can understand and understand. Follow the tutorial and take you to learn Python systematically from zero basics!

1. Learning routes in all directions of Python

The route of all directions in Python is to organize the commonly used technical points of Python to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
insert image description here
2. Python learning software

If a worker wants to do a good job, he must first sharpen his tools. The commonly used development software for learning Python is here!
insert image description here
3. Python introductory learning video

There are also many learning videos suitable for getting started with 0 basics. With these videos, you can easily get started with Python~insert image description here

4. Python exercises

After each video lesson, there are corresponding practice questions, you can test the learning results haha!
insert image description here

Five, Python actual combat case

Optical theory is useless. You have to learn to type codes along with it, and then you can apply what you have learned in practice. At this time, you can learn from some practical cases. This information is also included~insert image description here

6. Python interview materials

After we have learned Python, we can go out and find a job with the skills! The following interview questions are all from top Internet companies such as Ali, Tencent, Byte, etc., and some Ali bosses have given authoritative answers. After reading this set of interview materials, I believe everyone can find a satisfactory job.
insert image description here
insert image description here
7. Information collection

The full set of learning materials for the above-mentioned full version of Python has been uploaded to the CSDN official website. Those who need it can scan the QR code of the CSDN official certification below on WeChat to receive it for free.

Guess you like

Origin blog.csdn.net/pythonhy/article/details/132147670