matplotlib基本画图学习

1. 普通绘图: plot(x,y)

2.设置坐标范围; plt.xlim()   plt.ylim()

3.设置xy轴的坐标标签: xlabel()   ylabel()

4.设置刻度:xticks()     yticks()

5.设置图例 legend()

6.scatter(x,y)散点图

7.text()加文本

8.bar()条形图

9.contourf()和contour()等高线

具体例子:

#coding:utf-8
import matplotlib.pyplot as plt 
import numpy as np 

x1 = np.linspace(-3,3,100) #从-1到1等间距生成100个数字
y1 = 2*x1+1
y2 = x1**2

#------------------------两个图分开显示----------------------------------------#
plt.figure (figsize= (5,5)) #创建一个图
plt.plot(x1,y1) #画图

plt.figure (figsize= (5,5))#创建一个图
plt.plot(x1,y2) #画图

#------------------------两个线画在一起----------------------------------------#
plt.figure (figsize= (5,5))#创建一个图

#规定xy坐标的范围
plt.xlim(-5, 5)
plt.ylim(-5, 5)

#设置坐标轴的标签
plt.xlabel ("X")
plt.ylabel ("Y")

#绘制数据
line_1 = plt.plot(x1,y1, color = "red", linewidth = 2, linestyle = "--", label = "2x+1") #画图,虚线
line_2 = plt.plot(x1,y2, color = "blue", linewidth = 2, linestyle = "-", label = "pow(x,2)") #画图,实线

#设置图例
plt.legend( loc ="lower right")

#设置xy轴的尺度标签:x轴为新的坐标,y轴标识在这些值的位置替换成字符串
new_sticks = np.linspace(-5,5,11)
plt.xticks (new_sticks)
plt.yticks ([-1,0,1,2,3],["level1", "level2","level3","level4","level5"])

#gca:get current axis
ax = plt.gca()
#将四个框的颜色设置一下
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.spines["left"].set_color("red")  #x轴
ax.spines["bottom"].set_color("red") #y轴

#移动一下xy轴的位置
ax.spines["left"].set_position(("data",0)) #x轴移到0的位置
ax.spines["bottom"].set_position(("data",0)) #y轴移到0的位置

#添加说明
x0 =1
y0 =2*x0+1
plt.scatter(x0,y0, s=50, color = "b")
plt.plot((x0, x0), (y0,0),  "k--", lw = 2)
#添加注释                    箭头位置         文本放置位置         文本为起点
plt.annotate(r"2x+1=%s"%y0, xy=(x0,y0),xytext = (+30,-30),textcoords="offset points", fontsize = 16,
arrowprops = dict(arrowstyle = '->',connectionstyle = "arc3,rad =.2" )) #弧度
#添加文本
plt.text(-5,-3, r"this is the text", fontdict = {"size":16, "color": "red"})

#新建一个图像---------------------散点图----------------------------------
plt.figure()

x3 = np.random.normal(0,1,100)
y3 = np.random.normal(0,1,100)
plt.scatter(x3,y3, s = 50, c = 'b', alpha = 0.5)

#新建一个图像---------------------条形图----------------------------------
plt.figure()

x4 = np.random.normal(0,10,100)
y4 = x4 ** 2+1
plt.bar(x4,y4, facecolor = "red", edgecolor = "black") #填充颜色, 边界颜色

#zip可以打包
for x,y in zip(x4,y4):     #水平位置     垂直位置
    plt.text(x,y, "%.2f"%y, ha ="center", va ="bottom")

#新建一个图像---------------------等高线图----------------------------------
plt.figure()
#准备数据
x5 = np.linspace(-3,3,100)
y5 = np.linspace(-3,3,100)
#格网化
X,Y = np.meshgrid (x5,y5)
def F(x,y):
    return (1-x/2 +x**5 +y**3 )*np.exp(-x**2-y**2)
#填充的等高线
plt.contourf(X, Y, F(X,Y), 10,alpha = 0.75, cmap = plt.cm.hot)
#空心的线
C = plt.contour(X, Y, F(X,Y), 10,colors = "black", linewidths = 5)
#标记等高线数字
plt.clabel (C, inline = True, fontsize = 10)

plt.xticks(())
plt.yticks(())

#新建一个图像---------------------画3D图----------------------------------
from mpl_toolkits.mplot3d import Axes3D
#新建一个图像
fig= plt.figure()
#准备一个三维坐标
ax = Axes3D(fig)
#产生数据
x6 = np.arange(-4,4, 0.25)
y6 = np.arange(-4,4, 0.25)
X6,Y6 =np.meshgrid(x6,y6)
R = np.sqrt(X6**2+Y6**2)
Z6 = np.sin(R)
#绘制三维图
ax.plot_surface(X6,Y6,Z6, rstride =1, cstride =1, cmap = plt.get_cmap("rainbow"))
#绘制底部投影(实际上就是一个等高线) 投影方向    投影的位置    渲染颜色
ax.contourf(X6, Y6, Z6, zdir= "z", offset = -3, cmap = "rainbow")
#设置坐标范围
ax.set_zlim(-3,3)

#------------------------------------一个figure显示多个图-----------------
plt.subplot(2,2,1)    #将整个图分为2行2列,在第1个位置画图
plt.plot(x1,y1)
plt.title("2x+1")  #加一个标题

plt.subplot(2,2,2)   #将整个图分为2行2列,在第2个位置画图
plt.plot(x1,y2)
plt.title("x*x")  #加一个标题

plt.subplot(2,3,4) #将整个图分为2行3列,在第4个位置画图
plt.scatter(x3,y3, s = 50, c = 'b', alpha = 0.5)

plt.subplot(2,3,5)   #将整个图分为2行3列,在第5个位置画图
plt.bar(x4,y4, facecolor = "red", edgecolor = "black") #填充颜色, 边界颜色

plt.subplot(2,3,6)  #将整个图分为2行3列,在第6个位置画图
#填充的等高线
plt.contourf(X, Y, F(X,Y), 10,alpha = 0.75, cmap = plt.cm.hot)
#空心的线
C = plt.contour(X, Y, F(X,Y), 10,colors = "black", linewidths = 5)
#标记等高线数字
plt.clabel (C, inline = True, fontsize = 10)


plt.show() #显示


 

执行结果: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值