版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lql0716/article/details/74617957
python matplotlib 画图
author@jason_ql(lql0716)
http://blog.csdn.net/lql0716
1、代码
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 06 21:59:39 2017
@author: lql0716
"""
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-20,20) #自变量x
y = x**2 #变量y
plt.figure(num = 3, figsize = (8, 12), dpi = 100) #设置画图的图片的大小,figsize大概为厘米尺寸,分辨率dpi
plt.plot(x, y, 'r*', label = 'line', linewidth = 3)
plt.title('Function') # 标题
plt.xlabel('x (cm)') # x轴的名称
plt.ylabel('y (cm)') # y轴的名称
xt = np.arange(-20,20, 10) #设置x轴刻度步长
plt.xticks(xt)
yt = np.arange(0, 400, 20) #设置y轴刻度步长
plt.yticks(yt)
plt.legend(loc = 0) #参数loc,设置label在图中的位置,含义如下
#'best' : 0, (only implemented for axes legends)(自适应方式)
#'upper right' : 1,
#'upper left' : 2,
#'lower left' : 3,
#'lower right' : 4,
#'right' : 5,
#'center left' : 6,
#'center right' : 7,
#'lower center' : 8,
#'upper center' : 9,
#'center' : 10,
plt.grid(True) #True表示显示网格线,其它参数linestyle 设置线显示的类型(一共四种), color 设置网格的颜色, linewidth 设置网格的宽度
#plt.grid(True, linestyle = "-.", color = "r", linewidth = "3")
plt.savefig("D:/photo/1-1.jpg") #保存绘制的图片
plt.show()