Matplotlib简单回顾

import numpy as np
from pylab import *
from matplotlib import pyplot as plt
 
x = [1, 2, 3, 4]
y = [3, 5, 10, 25]
 
#创建Figure
fig = plt.figure()
 
#创建一个或多个子图(subplot绘图区才能绘图)
ax1 = fig.add_subplot(231)
plt.plot(x, y, marker='D') #绘图及选择子图
plt.sca(ax1)
  
ax2 = fig.add_subplot(232)
plt.scatter(x, y, marker='s', color='r') 
plt.sca(ax2)
plt.grid(True)
 
ax3 = fig.add_subplot(233)
plt.bar(x, y, 0.5, color='c') #柱状图 width=0.5间距
plt.sca(ax3)
 
ax4 = fig.add_subplot(234) 
#高斯分布   
mean = 0  #均值为0   
sigma = 1 #标准差为1 (反应数据集中还是分散的值)  
data = mean+sigma*np.random.randn(10000)
plt.hist(data,40,normed=1,histtype='bar',facecolor='yellowgreen',alpha=0.75)
plt.sca(ax4)
 
m = np.arange(-5.0, 5.0, 0.02)
n = np.sin(m)
ax5 = fig.add_subplot(235)
plt.plot(m, n)
plt.sca(ax5)
 
ax6 = fig.add_subplot(236)
xlim(-2.5, 2.5) #设置x轴范围
ylim(-1, 1)     #设置y轴范围
plt.plot(m, n)
plt.sca(ax6)
plt.grid(True)
 
plt.show()
'''1.plot(x, y, marker='D')表示绘制折线图,marker设置样式菱形。
    2.scatter(x, y, marker='s', color='r')绘制散点图,红色正方形。
    3.bar(x, y, 0.5, color='c')绘制柱状图,间距为0.5,原色。
    4.hist(data,40,normed=1,histtype='bar',
                   facecolor='yellowgreen',alpha=0.75)直方图。
    5.设置x轴和y轴的坐标值:
      xlim(-2.5, 2.5) #设置x轴范围 ylim(-1, 1) #设置y轴范围
    6.显示中文和负号代码如下:
      plt.rcParams['font.sas-serig']=['SimHei'] #用来正常显示中文标签
      plt.rcParams['axes.unicode_minus']=False #用来正常显示负号'''

  

posted @ 2018-12-08 17:46  萧白白  阅读(191)  评论(0编辑  收藏  举报