1.基础工作
导入必备的包,通常需要pandas、numpy和matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
设置坐标轴格式,从matplotlib.ticker中引入相应的包进行坐标轴的设置,当然这一步不是必须的,matplotlib会根据数据自动设置坐标轴的大小,大家也可以根据需要自己进行坐标轴的设置。
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
以x轴坐标设置为例,对用法进行说明:
xmajorLocator = MultipleLocator(pi/4) #将x主刻度标签设置为pi/4的倍数
xmajorFormatter = FormatStrFormatter('%1.1f') #设置x轴标签文本的格式
xminorLocator = MultipleLocator(pi/8) #将x轴次刻度标签设置为pi/8的倍数
2.创建画布和画板
(1)创建一个画布fig=plt.figure()
figure函数的基本用法为figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
其中,num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
最重要的参数就是figuresize,用于指定画布的大小,一般不省略这个参数。
(2)创建一个坐标系
ax=figure.subplot(111)
subplot函数的基本用法为subplot(nrows, ncols, index, **kwargs)
其中nrows为子图行数,ncols为子图列数,index为子图索引,从1到nrows*ncols,通常采用简写形式表示这三个参数,如subplot(1,