Python 用 matplotlib 中的 plot 画图

转载自:https://www.cnblogs.com/xianhan/p/9131156.html

保存图片

plt.savefig('./waveform/my.png',dip=200)
    plt.show()

 设置图片大小

fig = plt.figure(figsize=(13.20, 7.75), dpi=100)

 

首先在python中使用任何第三方库时,都必须先将其引入。即:

import matplotlib.pyplot as plt

或者:

from matplotlib.pyplot import *

1.建立空白图

fig = plt.figure()

也可以指定所建立图的大小

fig = plt.figure(figsize=(4,2))

也可以建立一个包含多个子图的图,使用语句:

plt.figure(figsize=(12,6)) plt.subplot(231) plt.subplot(232) plt.subplot(233) plt.subplot(234) plt.subplot(235) plt.subplot(236) plt.show()

其中subplot()函数中的三个数字,第一个表示Y轴方向的子图个数,第二个表示X轴方向的子图个数,第三个则表示当前要画图的焦点。

可以看到图中的x,y轴坐标都是从0到1,当然有时候我们需要其他的坐标起始值。 
此时可以使用语句指定:

ax1.axis([-1, 1, -1, 1])

或者:

plt.axis([-1, 1, -1, 1]

2.向空白图中添加内容,想你所想,画你所想

 

首先给出一组数据:

x = [1, 2, 3, 4, 5] y = [2.3, 3.4, 1.2, 6.6, 7.0]

A.画散点图*

plt.scatter(x, y, color='r', marker='+') plt.show()

这里的参数意义:

  1. x为横坐标向量,y为纵坐标向量,x,y的长度必须一致。
  2. 控制颜色:color为散点的颜色标志,常用color的表示如下:

    b---blue c---cyan g---green k----black m---magenta r---red w---white y----yellow

    有四种表示颜色的方式:

    • 用全名
    • 16进制,如:#FF00FF
    • 灰度强度,如:‘0.7’
  3. 控制标记风格:marker为散点的标记,标记风格有多种:

    .  Point marker
    ,  Pixel marker
    o  Circle marker
    v  Triangle down marker 
    ^  Triangle up marker < Triangle left marker > Triangle right marker 1 Tripod down marker 2 Tripod up marker 3 Tripod left marker 4 Tripod right marker s Square marker p Pentagon marker * Star marker h Hexagon marker H Rotated hexagon D Diamond marker d Thin diamond marker | Vertical line (vlinesymbol) marker _ Horizontal line (hline symbol) marker + Plus marker x Cross (x) marker

B.函数图(折线图)

数据还是上面的。

fig = plt.figure(figsize=(12, 6)) plt.subplot(121) plt.plot(x, y, color='r', linestyle='-') plt.subplot(122) plt.plot(x, y, color='r', linestyle='--') plt.show()

这里有一个新的参数linestyle,控制的是线型的格式:符号和线型之间的对应关系

-      实线
-- 短线 -. 短点相间线 : 虚点线

另外除了给出数据画图之外,我们也可以利用函数表达式进行画图,例如:y=sin(x)

from math import *
from numpy import *
x = arange(-math.pi, math.pi, 0.01) y = [sin(xx) for xx in x] plt.figure() plt.plot(x, y, color='r', linestyle='-.') plt.show()

C.扇形图

示例:

import matplotlib.pyplot as plt
y = [2.3, 3.4, 1.2, 6.6, 7.0] plt.figure() plt.pie(y) plt.title('PIE') plt.show()

D.柱状图bar

示例:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5] y = [2.3, 3.4, 1.2, 6.6, 7.0] plt.figure() plt.bar(x, y) plt.title("bar") plt.show()

E.二维图形(等高线,本地图片等)

import matplotlib.pyplot as plt
import numpy as np import matplotlib.image as mpimg # 2D data delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z = Y**2 + X**2 plt.figure(figsize=(12, 6)) plt.subplot(121) plt.contour(X, Y, Z) plt.colorbar() plt.title("contour") # read image img=mpimg.imread('marvin.jpg') plt.subplot(122) plt.imshow(img) plt.title("imshow") plt.show() #plt.savefig("matplot_sample.jpg")

F.对所画图进行补充

__author__ = 'wenbaoli'


import matplotlib.pyplot as plt from math import * from numpy import * x = arange(-math.pi, math.pi, 0.01) y = [sin(xx) for xx in x] plt.figure() plt.plot(x, y, color='r', linestyle='-') plt.xlabel(u'X')#fill the meaning of X axis plt.ylabel(u'Sin(X)')#fill the meaning of Y axis plt.title(u'sin(x)')#add the title of the figure plt.show()

如果我们想自定义坐标轴的标题,坐标轴的刻度,坐标轴刻度的范围,设置图形标题,添加图例时,可以通过设置 pyplot 函数中的 xlable(横坐标轴标题),
ylabel(纵坐标轴标题), xticks(横坐标轴刻度),yticks(纵坐标轴刻度),title(图形标题), grid(显示网格),legend(显示图例)等属性来实现

多条曲线画在一个图上,建立一个figure,然后使用多次plot即可,分开画就一个figure配一个plot即可。






你可能感兴趣的:(Python 用 matplotlib 中的 plot 画图)