Home>Article>Backend Development> From 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.

coldplay.xixi
coldplay.xixi forward
2020-09-14 13:45:03 2116browse

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 usedMATLAB, 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 toolkitin 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 torename 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 makeline graphs or curve graphs, which is called plot. The second type is to make abar chartcalled hist, the third type is to make ascatter chartand is called scatter, and the fourth type is to make apie chartof. 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 pointsPresent, 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 thephp trainingcolumn!

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.im. If there is any infringement, please contact admin@php.cn delete