Matplotlib学习(三)--其他格式数据可视化

scatter 散点数据

import numpy as np
import matplotlib.pyplot as plt

n = 1024
x = np.random.normal(0,1, n) #标准正态分布
y = np.random.normal(0,1,n)
t = np.arctan2(x, y)  #随机生成颜色

plt.scatter(x, y, c=t, s=75, alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
plt.xticks(()) #隐藏x轴
plt.yticks(()) #隐藏y轴

plt.show()

结果如下图所示:

bar 柱状图

import numpy as np
import matplotlib.pyplot as plt

n = 12
x = np.arange(n)
y1 = (1-x/float(n)) * np.random.uniform(0.5,1.0,n)
y2 = (1-x/float(n)) * np.random.uniform(0.5,1.0,n)

#设置颜色
plt.bar(x,y1,facecolor='#9999ff',edgecolor='white')
plt.bar(x,-y2,facecolor='#ff9999',edgecolor='white') #向下的柱状图

for x1,y in zip(x,y1): #zip起来后才能每次分配给x和y两个值
    #ha:horizontal alignment va:veical alignment
    plt.text(x1, y+0.05, '%.2f'%y, ha='center', va='bottom')

for x1,y in zip(x,y2): #zip起来后才能每次分配给x和y两个值
    #ha:horizontal alignment va:veical alignment
    plt.text(x1, -y-0.05, '-%.2f'%y, ha='center', va='top')

    
plt.xlim(-0.5,n)
plt.xticks(())
plt.ylim(-1.25, 1.25)
plt.yticks(())

plt.show()

结果如下图所示:

contour 等高线图

import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    # the height function
    return (1-x/2 + x**5 + y**3)*np.exp(-x**2-y**2)

n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
X,Y = np.meshgrid(x,y)

#use plt.contourf to filling contours
#X, Y and value for(X,Y) point

#cmap hot:每个数字对应到一个颜色   8是指分为10份 0为分2份
plt.contourf(X, Y, f(X, Y),10, alpha=0.75, cmap=plt.cm.hot)

# use plt.contour to add contour lines
C = plt.contour(X,Y,f(X,Y),10,colors='black',linewidths=0.5)

#adding label
plt.clabel(C,inline=True,fontsize=10)

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

结果如下图所示:

Image 图片

import numpy as np
import matplotlib.pyplot as plt

a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
              0.365348418405, 0.439599930621, 0.525083754405,
              0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
plt.imshow(a, interpolation='nearest',cmap='bone', origin='upper')
#origin: upper是指图像和矩阵数值一一对应,lower会反过来
plt.colorbar(shrink=0.9) #shrink:高度压缩为原来的90%

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

结果如下图所示:


 3D plot 3D数据

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)   #显示三维 axes
X = np.arange(-4,4,0.25)
Y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(X,Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
#height value

ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'),edgecolor='black')
#rstride 和 cstride 是行和列的跨度

ax.contourf(X,Y,Z,zdir='z',offset=-1,cmap='rainbow')
#zdir:从哪个轴压下去  offset:压到z=几的面上

plt.show()

结果如下图所示:


 Subplot 多合一显示

import numpy as np
import matplotlib.pyplot as plt


#分为2行,但第一个图占据一行

plt.figure()

plt.subplot(2,1,1) #把figure分为2行1列,然后在第1个位置plot
plt.plot([0,1],[0,1])

plt.subplot(2,3,4)  #把figure分为2行3列,然后在第4个位置plot
plt.plot([0,1],[0,1])

plt.subplot(2,3,5)
plt.plot([0,1],[0,1])

plt.subplot(2,3,6)
plt.plot([0,1],[0,1])

plt.show()

结果如下图:


 subplot in grid 分格显示

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

#method 1: subplot2grid
########################
plt.figure()
ax1 = plt.subplot2grid((3,3),(0,0), colspan=3, rowspan=1)
#第一个参数整体分格,第二个参数表示ax1的起点,span指行列跨度
ax1.plot([1,2], [1,2])
#ax1.set_xlabel()#设置子图的label
ax1.set_title('ax1_title') #都变成了set

ax2 = plt.subplot2grid((3,3),(1,0), colspan=2, rowspan=1)
ax3 = plt.subplot2grid((3,3),(1,2), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3,3),(2,0), colspan=1, rowspan=1)
ax5 = plt.subplot2grid((3,3),(2,1), colspan=1, rowspan=1)

#method 2: gridspec
########################
plt.figure()
gs = gridspec.GridSpec(3,3) #3行3列
ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,:2])
ax3 = plt.subplot(gs[1:3,2])
ax4 = plt.subplot(gs[2,0])
ax5 = plt.subplot(gs[2,1])

#method 3: easy to define structure
########################
f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True) #2行2列,共享x轴和y轴
ax11.scatter([1,2],[1,2])
plt.tight_layout()

plt.show()

三种方法对应的结果如下图所示:

图1

 图2

 图3


plot in plot 图中图

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
x = [1,2,3,4,5,6,7]
y = [1,3,4,2,5,8,6]

left,bottom,width,height = 0.1,0.1,0.8,0.8  #相对于figure的左下点和高宽
ax1 = fig.add_axes([left,bottom,width,height])
ax1.plot(x,y,'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

left,bottom,width,height = 0.2,0.6,0.25,0.25  #子图1
ax2 = fig.add_axes([left,bottom,width,height])
ax2.plot(y,x,'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

plt.axes([0.6,0.2,0.25,0.25])   #子图2
plt.plot(y[::-1],x,'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

plt.show()

结果如下图所示:


 secondary axis 次坐标

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y1 = 0.05*x**2
y2 = -1*y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()  #使用镜面将ax1的数据翻过来
ax1.plot(x,y1,'g-')
ax2.plot(x,y2,'b-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1',color='g')
ax2.set_ylabel('Y2',color='b')

plt.show()

 

animation 动画

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line,=ax.plot(x,np.sin(x))  #打逗号是只选择列表第一位

def animate(i):
    line.set_ydata(np.sin(x+i/10))  #更新方式 i为时间
    return line,

def init():
    line.set_ydata(np.sin(x))
    return line,

ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=20,
                              blit=True)
#frames 多少帧  init_func:最开始的一帧是什么 interval:多少ms更新一次
#blit:只更新变化的就是True,False是全部更新

plt.show()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值