search
HomeBackend DevelopmentPython TutorialFrom now on, you can control charts with matplotlib, a powerful drawing tool in Python.

From now on, you can control charts with matplotlib, a powerful drawing tool in Python.

Related learning recommendations: python tutorial

Let me first introduce to you another very useful data analysis library ——matplotlib.



Introduction to matplotlib

If you are attending in college If you have participated in a mathematical modeling competition or used MATLAB, you will be more impressed by the drawing function in this software. MATLAB can make various functions and numerical distribution images very easy and convenient. It doesn’t matter if you haven’t used it, it’s good to know this. Although MATLAB is easy to use, it is a paid software after all, and many people prefer Python's syntax to MATLAB.

So MATLAB was taken into consideration. Later, a master imitated the drawing tool in MATLAB and developed a similar drawing tool in Python. This is the matplotlib we are going to talk about in this article today. It is also the most widely used drawing toolkit in the fields of Python, data analysis and machine learning, almost none of them. Some time ago, the U.S. government was involved in something that caused MATLAB to announce a ban on the use of MATLAB in several universities in China. Some people suggested that we could use some open source tools in Python instead. Of course, there are many other powerful tools in MATLAB. Function, Python may not necessarily be able to replace it, but at least it is no problem in the field of graphics.

Basic usage of matplotlib

matplotlib is widely popular The main reason is that it is convenient and easy to use. Let’s take a look at a few classic examples. You can create very high-quality pictures in just a few lines of code.

We are using the pyplot library under the matplotlib package. We can also see from the name that it is a Python library. If you have installed a complete Python package management tool like Anaconda, this package should already be installed by default. If not, you can use pip to install it. The command is also very simple.

pip install matplotlib复制代码

After installing the package, our usual usage practice is to rename it to plt, so if you read some source code and the code of other masters, plt will often appear when drawing. xxx, that’s how it came about.

import matplotlib.pyplot as plt复制代码

There are several commonly used drawing tools in plot. The first one is to make line graphs or curve graphs, which is called plot. The second type is to make a bar chart called hist, the third type is to make a scatter chart and is called scatter, and the fourth type is to make a pie chart of. In addition, you can also create some three-dimensional images, but they are rarely used in daily life and are mostly used to show off your skills.

Curve graph

plot is actually used to make curve graphs. The method used is to draw points Present, it will connect our data points into a smooth curve. Of course, polylines and straight lines are also possible. The final effect of the graph is completely determined by our data.

Its usage is very simple. In the most basic usage, we only need to pass in two lists, representing the values ​​of the x-axis and y-axis respectively. For example, we want to make a logistic function image. We can do this:

x = np.linspace(-10, 10, 100)y = 1 / (1 + np.exp(-x))plt.plot(x, y)plt.show()复制代码

In the above code, we first use the linspace function in numpy to generate a numpy array between -10 and 10. There are 100 points in this array, evenly distributed between -10 and 10. We calculate the value of the corresponding logistic function for it, which is

. I think everyone should be familiar with this function. x is a list of one hundred points, and the corresponding y is also a list of one hundred points. That is to say, an x ​​corresponding to a y forms a point pair, which represents a coordinate in the picture.

We don’t need to do any conversion, we just need to pass these two arrays into plt, and it will automatically complete the coordinate mapping for us. The final effect is as follows.

We can see that the drawing effect is quite good. In addition to drawing the complete image, we also drew points on the coordinate axis for us. It is convenient for us to view the data.

Histogram

hist can be used to draw

histogram, which is why we are very Common images. In the financial field, we often use histograms to reflect the trend of a data. In data analysis and machine learning, we also often use histograms to reflect the distribution of a certain feature or value.

在直方图当中我们只需要传入一个数组,就是我们要求分布的数组,另外我们需要再输入一个参数bins,表示我们希望将数据分桶的数量。然后它会绘制出每个桶中数据数量的直方图,这样我们就可以很直观地看出分布来了。

x = np.random.normal(size=200)plt.hist(x,bins=30)plt.show()复制代码

这里我们调用numpy当中以正态分布概率随机的函数,得到的结果应该是正态分布的。

我们查看图像,这个结果和我们的预期基本吻合。

点状图

第三种图像叫做散点图,它反应的是每个点的分布情况。比如我们之前在介绍KNN和Kmeans模型的时候曾经都作过散点图,一个点代表一个样本,通过散点图我们观察的也是样本和数据分布的情况。但是和直方图相比它更加得直观,我们能直接看到分布,而不是一个分布的统计结果。

但是它也有缺点,缺点就是我们只能观察二维平面当中的点的分布,毕竟超过3维的维度就没办法绘制了。而且3维的数据分布也不是很好观察。

同样散点图的绘制也很简单,我们只需要调用scatter函数传入x和y的数组即可。

x = np.random.rand(100)y = np.random.rand(100)plt.scatter(x, y)复制代码

这里我们的x和y都是随机分布,所以得到的结果是散乱没有规律的点。

饼状图

最后我们来看下饼状图,饼状图也很常用,虽然也是反映的数据分布,但是它更多是体现某一个值占的比例。通过饼状图,我们可以很直观地了解到这点。

我们可以传入一个list进入pie函数,plot会根据list当中值在总体的占比绘制饼状图。我们来看个很简单的例子:

a = np.array([0.5, 0.3, 0.2, 0.1, 0.8])plt.pie(a)复制代码

得到的结果如下:

Compared with the previous results, the pie chart is slightly unclear, because although it uses different colors to distinguish different areas, it is difficult for us to visually see each area. What do they represent? This problem can be solved. We can pass in some parameters and settings to make it display more information and make the data more vivid and clear.

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of From now on, you can control charts with matplotlib, a powerful drawing tool in Python.. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software