matplotlib绘图学习

matplotlib绘图学习

(1)matplotlib安装

  下载地址https://pypi.python.org/pypi/matplotlib#downloads
  下载windows包matplotlib-2.1.0-cp35-cp35m-win_amd64.whl
  安装命令:
    python -m pip --user matplotlib-2.1.0-cp35-cp35m-win_amd64.whl
  检查是否安装成功使用import导入操作,不报错即可


(2)绘制一个简单的折线图

  import matplotlib.pyplot as plt
  #注意运行试验的python脚本的名称命名为模块的名称,import的时候会出错

  squares = [1, 4, 9, 16, 25]
  plt.plot(squares)    #传递需要生成图形的列表信息
  plt.show()         #利用设置的信息绘制图形


  设置标签和线条

  plt.plot(squares, linewidth=5)

  plt.title("Square Numbers", fontsize=24)    #设置标题
  plt.xlabel("Value", fontsize=14)        #设置x轴的标签和字体
  plt.ylabel("Square of value", fontsize=14)     #设置y轴的标签和字体

  plt.tick_params(axis='both', labelsize=14)    #设置刻度的样式

(3)绘制散点图

  plt.scatter(2, 4)                #绘制一个坐标点

  x_values = [1, 2, 3, 4, 5]
  y_values = [1, 4, 9, 16, 25]
  plt.scatter(x_values, y_values, s=100)     #接受x,y列表参数,s定义坐标点的尺寸大小

  plot.axis([0, 1100, 0, 1100000])        #设置每个坐标的取值范围

(4)设置散点图的颜色


  matplotlib设置散点为蓝色点和黑色轮廓,可以进行自定义设置
  plt.scatter(x_values, y_values, edgecolor='none', s=40)     #edgecolor设置为none可以消除数据点轮廓,但是2.0之后指定设置的none

  #可以使用RGB自定义的设置数据点的颜色

  plt.scatter(x_values, y_values, c=(0,0,0.8) edgecolor='none', s=40)

  #使用颜色映射

  plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolor='none', s=40)

(5)保存图表

  plt.savefig(r"C:UsersAdministratorDesktopsquares_plot.png", bbox_incges='tight')

(6)创建一个随机漫步的图表

  使用类方法来实现这个任务,需要使用random模块

  #random_walk.py

 1 from random import choice
 2 
 3 class Random_walk() :
 4     
 5     def __init__(self, num_point=5000) :
 6         
 7         self.num_point = num_point
 8         self.x_values = [0]
 9         self.y_values = [0]
10 
11     def fill_walk(self) :
12         while len(self.x_values) < self.num_point :
13             x_direction = choice([1,-1])
14             x_distance = choice([1, 2, 3, 4])
15             x_step = x_direction * x_distance
16             
17             y_direction = choice([1,-1])
18             y_distance = choice([1, 2, 3, 4])
19             y_step = y_direction * y_distance
20             
21             if x_step == 0 and y_step == 0 :
22                 continue
23                 
24             next_x = self.x_values[-1] + x_step
25             next_y = self.y_values[-1] + y_step        
26             
27             self.x_values.append(next_x)
28             self.y_values.append(next_y)

  #random_main.py

from random_walk import Random_walk

import matplotlib.pyplot as plt

rw = Random_walk()
rw.fill_walk()

plt.scatter(rw.x_values, rw.y_values, s=5)
plt.show()

  #增加一个可以进行多次绘制随机漫步图的逻辑

while True :
    
    ....
      
    keep_running = input("Make another walk? (y/n): ")    
    if keep_running == n :
        break

  #按照点绘制的顺序进行由浅至深的绘制坐标
  point_number = list(range(rw.num_point))
  plt.scatter(rw.x_values, rw.y_values, c=point_number, cmap=plt.cm.Blues, s=10)

  

  #将起点和重点突出出来  

  plt.scatter(0, 0, c='green', s=5)
  plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', s=5)

 

  #隐藏坐标轴
  plt.axes().get_xaxis().set_visible(False)
  plt.axes().get_yaxis().set_visible(False)

  #设置绘图窗口的尺寸
  plt.figure(dpi=128, figsize=(10, 6)) #figsize可用于指定图表的宽度,高度,分辨率和背景色

原文地址:https://www.cnblogs.com/solitarywares/p/7901144.html