Matplotlib简单使用(实例)

原文地址

分类目录——Matplotlib

导入支持包

import matplotlib.pyplot as plt
import numpy as np

生成测试数据

# 生成测试数据
x = np.linspace(-3, 3, 50)
y1 = 2*x + 1
y2 = x**2

生成画布

# 生成画布
plt.figure(facecolor='grey')

# figure()中的属性
'''
**num=3**
    画出来图的标题就是‘Figure 3’
    如果传一个str,eg.    num='折线图'   图的标题就是‘折线图’

**figsize=(8, 4)**
    指定绘图对象的宽度和高度,单位为英寸,一英寸=80px
    
**facecolor='blue'**
    背景颜色,默认是白色
    也可以以‘#+6位16进制数’给出,eg.   '#00ff00'
    
**edgecolor='red'**
    边框颜色,默认是白色
'''

画图

# 画图
plt.plot(x, y1, linestyle='--')
plt.plot(x, y2)

# plot的属性
'''
**linewidth=3**
    线条宽度
    也可以写作   lw=3

**markersize='20'**
    线上标记的尺寸
    注意要传字符串类型的值

**marker='2'**
    线上的标记
    =============    ===============================
    character        description
    =============    ===============================
    ``'.'``          point marker
    ``','``          pixel marker
    ``'o'``          circle marker
    ``'v'``          triangle_down marker
    ``'^'``          triangle_up marker
    ``'<'``          triangle_left marker
    ``'>'``          triangle_right marker
    ``'1'``          tri_down marker
    ``'2'``          tri_up marker
    ``'3'``          tri_left marker
    ``'4'``          tri_right marker
    ``'s'``          square marker
    ``'p'``          pentagon marker
    ``'*'``          star marker
    ``'h'``          hexagon1 marker
    ``'H'``          hexagon2 marker
    ``'+'``          plus marker
    ``'x'``          x marker
    ``'D'``          diamond marker
    ``'d'``          thin_diamond marker
    ``'|'``          vline marker
    ``'_'``          hline marker
    =============    ===============================

**linestyle=':'**
    线的类型
    =============    ===============================
    character        description
    =============    ===============================
    ``'-'``          solid line style
    ``'--'``         dashed line style
    ``'-.'``         dash-dot line style
    ``':'``          dotted line style
    =============    =============================== 
    也可表示为linestyle='dashed'   

**colors='r'**

    The supported color abbreviations are the single letter codes

    =============    ===============================
    character        color
    =============    ===============================
    ``'b'``          blue
    ``'g'``          green
    ``'r'``          red
    ``'c'``          cyan
    ``'m'``          magenta
    ``'y'``          yellow
    ``'k'``          black
    ``'w'``          white
    =============    ===============================
    也可用'#ff0000'这种形式表示
'''

显示

# 必须要有这一句画图才能显示
plt.show()

生成图片如下

1581431242040

发布了102 篇原创文章 · 获赞 68 · 访问量 5108

猜你喜欢

转载自blog.csdn.net/BBJG_001/article/details/104291154
今日推荐