Comprehensive learning of matplotlib

1. arange function

The arange function requires three parameters, which are the start point, end point, and sampling interval. The sampling interval defaults to 1

See example:

import numpy as np
#import matplotlib.pyplot as plt
x=np.arange(-5,5,1)
print(x)

2. Draw the sin(x) curve

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-10,10,0.1)
y=np.sin(x)
plt.plot(x,y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
This part is to import the numpy library, and then it is called np
to import matplotlib.pyplot (a function collection that looks like MATLAB), and several nicknames are called plt
.
plt.show(), display pictures.
According to the first title of the text, we use the arange function. It can be seen that (-10, 10, 0.1) the first two numbers determine the range of the horizontal axis, and the last number is the precision (the larger the number, the rougher it is). Let me take a look What a precision of one looks like.

you could say so. . . Not very aesthetically pleasing.

3. Add a title to sin() to control the x and y axes

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-10,10,0.1)
y=np.sin(x)
plt.xlim(-10,10)
plt.ylim(-3,3)
plt.xlabel("x")
plt.ylabel("y=x^3")
plt.title("$y=x^3$")
plt.plot(x,y)
plt.show()
plt.xlim, this is a function to control the range of the x-axis, which is fixed and cannot be changed. Similarly, plt.ylim is the same.
plt.ylabel, this is the function to fill in the title for the y-axis, as above.
plt.title, fill in the title, at the top and middle position, it can be seen that the "$" is not displayed.

4.linspace function

The linspace function performs sampling. The linspace function specifies the start value, end value, and number of elements to create a one-dimensional array. You can specify whether to include the end value through the keyword endpoint=True. The default setting is to include the end value.
I don’t understand, here’s an example ——>
import numpy as np
#import matplotlib.pyplot as plt
x=np.linspace(0,10,5)
print(x)
y=np.linspace(1,10,10)
print(y)
Each number in x differs by 2.5;
each number in y differs by 1.

5. Use the linspace function to draw a sin()

import numpy as np
from pylab import *
x=np.linspace(-6,6,100)
sin1=np.sin(x)
xticks(np.linspace(-5,5,5),('-5','-2.5','0','2.5','5'))
plot(x,sin1,color='blue',linewidth=2.0,linestyle=':')
show()
pylab中包括了很多numpy和pyplot中的常用函数;
xticks(np.linspace(-5,5,5),('-5','-2.5','0','2.5','5')):这个我感觉就是控制x轴的,后面括号的五个数字换啥都行,对应的就是横轴里面的五个坐标(是叫坐标吧),其实没有后面的括号也行,系统还是会匹配这五个数字。
plot(x,sin1,color='blue',linewidth=2.0,linestyle=':'):前两个数就相当于原来的xy,设置蓝色的线,线的宽度是2,线的样式是...的。
(可以选择这些样式:'-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted')。

6.书接上回绘制一条曲线

import numpy as np
from pylab import *
x=np.linspace(-6,6,100)
sin1=np.sin(x)
xticks(np.linspace(-5,5,5),('-5','-2.5','0','2.5','5'))
plot(x,sin1,color='blue',linewidth=2.0,linestyle=':')
show()

7.一张图片上绘制两条曲线

import numpy as np
from pylab import *
x=np.linspace(-6,6,100)
sin1=np.sin(x)
cos1=np.cos(x)
xticks(np.linspace(-5,5,5),('-5','-2.5','0','2.5','5'))
plot(x,sin1,color='blue',linewidth=2.0,linestyle=':')
plot(x,cos1,color='green',linewidth=2.0,linestyle='-')
show()
如上图所示两条曲线就是复制对应曲线的代码,很简单,但是缺点什么,那就是图例,要不然我怎么知道那条曲线是干什么的。

8.图例添加

import numpy as np
from pylab import *
x=np.linspace(-6,6,100)
sin1=np.sin(x)
cos1=np.cos(x)
xticks(np.linspace(-5,5,5),('-5','-2.5','0','2.5','5'))
plot(x,sin1,color='blue',linewidth=2.0,linestyle=':',label='sin(x)')
plot(x,cos1,color='green',linewidth=2.0,linestyle='-',label='cos(x)')
legend(loc='lower left')
show()
唯一和标题2,就多了几个代码:
label='sin(x)'【这句话是给图例加文字用的】
legend(loc='lower left')【这是控制图例位置的】
接下来引入下一个标题:legend函数

9.legend用法

我这里主要是将图例的摆放位置:
best 中文最好的,电脑自己选呗
upper right 右上
upper left 左上
lower left 左下
lower right 右下
right 中间最右
center left 中央偏左
center right 中央偏右
lower center 中央偏下
upper center 中央偏上
center 中央
最好把,是自己上手试一下,这些数据是哪来的呢,我直接写错(͡° ͜ʖ ͡°),程序报错了,电脑直接就告诉我都有啥了(͡° ͜ʖ ͡°)。。

10.画饼图

import numpy as np
import matplotlib.pyplot as plt
data=[1,2,3,4,2]
print(data)
plt.pie(data,explode=[0,0,0,0,0])
plt.show()
data【存放数据,这里放几个数体现在图中就是几个区域】;
那个print没啥用,测试随机数的时候写的,忘删除了;
plt.pie(data,explode=[0,0,0,0,0]),画饼图用的,explode这个参数里面的每个数字不是零之后,都会有一块对应的区域“飞起来”。下面展示一下啊。
import numpy as np
import matplotlib.pyplot as plt
data=[1,2,3,4,2]
print(data)
plt.pie(data,explode=[0,0,0.3,0,0])
plt.show()
那个数字也可以是负数呢,我把第三个数字换成-0.5看看奥。

可以,但是不好。

对了我上头说过随机数,就是把data换成随机生成的列表。
替换的语句是:
data=np.random.randint(1,8,5)
第一个数是随机数的下线(大于等于),第二个数是上限(小于等于),最后一个数是随机出来几个数(五个)。
import numpy as np
import matplotlib.pyplot as plt
data=np.random.randint(1,8,5)
print(data)
plt.pie(data,explode=[0,0,0.5,0,0])
plt.show()

11.散点图

import numpy as np
from pylab import  *
a=np.random.normal(0,10,100)
b=np.random.normal(0,1,100)
scatter(a,b,s=10,c='green')
show()
绘制散点图要注意num(a)=num(b),即a的数量等于b的数量;
scatter(x,y,s=10,c='green'),用来绘制散点图的函数,s代表散点图中(点)圆圈的大小,c代表圆圈的的颜色。
np.random.normal(0,1,100):提供服从正态分布的数据,看下面的例子:
import numpy as np
y=np.random.normal(0,1,100)
print(y)
0:正态分布的均值,0就是y轴的位置。
1:正态分布的标准差,数字越大,正态分布越矮胖;数字越小,正太分布曲线越高瘦。
中间这个数据可以是零,大不了点都集中在中心呗,负数不可以。
100:数据数量,上图中提供100个数据。

12. 柱图

import numpy as np
import  matplotlib.pyplot as plt
plt.figure(figsize=(6,5))
data = np.random.randint(1,8,10)
print(data)
x= np.arange(len(data))
plt.bar(x+1,data,alpha=1,color='green',width=0.2)
plt.show()
加标题等情况使用中文:
import matplotlib as mpl
mpl.rcParams['font.family']='sans-serif'#显示无衬线字体
mpl.rcParams['font.sans-serif']=[u'SimHei']#显示中文
上面的和下面的这几句不用都使用,使用一个就行,要是不行,那就再想想办法吧。
plt.rcParams["font.sans-serif"] = ["SimHei"]# 正确显示中文和负号
plt.rcParams["axes.unicode_minus"] = False
plt.bar (x,data,alpha=1,color='green',width=0.2)
分别是x轴,y轴, 柱形颜色深浅,柱形的颜色,柱形的宽度

x= np.arange(len(data))

13.柱形图加折线

import numpy as np
import  matplotlib.pyplot as plt
plt.figure(figsize=(6,5))
data = np.random.randint(1,8,10)
print(data)
x= np.arange(len(data))
plt.plot(x+1,data,color='r')
plt.bar(x+1,data,alpha=1,color='green',width=0.5)
plt.show()
plt.plot(x+1,data,color='r'),画折线的代码,加上即可。

14.简单三维图

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
sd = fig.add_subplot(111, projectinotallow='3d')#111,221,222,223,224
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
sd = fig.add_subplot(111, projectinotallow='3d')#111,221,222,223,224
X = [0, 1, 2, 1, 2, 4]
Y = [0, 4, 4, 1, 3, 4]
Z = [0, 4, 0, 0, 2, 4]
sd.plot_trisurf(X, Y, Z)
plt.show()
这个3d图可以转动,方便观察;
第四行代码:111,就是全屏或者或是正中间,剩下(221、222、223、224)对应四个角落。
plot_trisurf(z,y,z,...) :画3d曲平面的函数。
x,y,z要竖着看,一列对应的是一个点的坐标。

15.三维曲面标题等设置

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["font.sans-serif"] = ["SimHei"]# 正确显示中文和负号
plt.rcParams["axes.unicode_minus"] = False
fig = plt.figure()
sd = fig.add_subplot(111, projectinotallow='3d')#111,221,222,223,224
X = [0, 1, 2, 1, 2, 4]
Y = [0, 4, 4, 1, 3, 4]
Z = [0, 4, 0, 0, 2, 4]
sd.set_xlabel('x轴')
sd.set_ylabel('y轴')
sd.set_zlabel('z轴')
plt.title('这是标题')
sd.plot_trisurf(X, Y, Z)
plt.show()
我就直接使用标题1里面的代码加工了,
sd.set_xlabel('x轴')#x轴函数
sd.set_ylabel('y轴')#y轴函数
sd.set_zlabel('z轴')#z轴函数
plt.title('这是标题')#添加标题函数
因为我使用了中文,
plt.rcParams["font.sans-serif"] = ["SimHei"]# 正确显示中文和负号
plt.rcParams["axes.unicode_minus"] = False
所以还得用这两行代码,要是仅仅使用英文的话删除即可。

Guess you like

Origin blog.csdn.net/weixin_53197693/article/details/129259539