如何画完整易读的统计图


绘图第一步,先导入库。

import matplotlib.pyplot as plt

先讲一些基础知识,后面会给出优秀示例,码住一起学习📚,未完待续……

画布设置

一般情况下,绘图前先设置画布大小plt.figure(figsize=(10, 5))
matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, *, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

  • frameon
    图例是否有边框。

横纵坐标和标题设置

一般情况下,一幅好看的统计图表,都需要给它命名,我将介绍Python中的plt.xlabel,plt.ylabel,plt.title

plt.xlabel()

matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)

  • xlabel
    字符串形式,即横坐标内容
  • loc
    'left', 'center', 'right'三种,默认居中'center'

plt.ylabel()

matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)

  • ylabel
    字符串形式,即纵坐标内容
  • loc
    'bottom', 'center', 'top'三种,默认居中'center'

plt.title()

matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs)[source]#

  • label
    字符串形式,即标题内容
  • fontdict

{‘fontsize’: 12,
‘fontweight’: rcParams[‘axes.titleweight’],
‘color’: rcParams[‘axes.titlecolor’],
‘verticalalignment’: ‘baseline’,
‘horizontalalignment’: loc}

  • loc
    'center', 'left', 'right'三种,默认居中

ax.set_xlim

Axes.set_xlim(left=None, right=None, *, emit=True, auto=False, xmin=None, xmax=None)

  • left/right
    浮点数,设置横坐标的起始结束位置。
  • xmin/xmax
    浮点数

ax.set_ylim

Axes.set_ylim(bottom=None, top=None, *, emit=True, auto=False, ymin=None, ymax=None)[source]

  • bottom/top
    浮点数,设置纵坐标的起始结束位置。
  • ymin/ymax
    浮点数

plt.suptitle()

图形正上方的标题,一般可用于多张图时。

plt.suptitle("Classification vector for...")

Markers

characterdescription
'.'
'o'
'v'倒三角
'^'正三角
'<'左三角
'>'右三角
'*'星型
'+'+号
'_'水平线
's'正方形
'x'x型
'X'x (filled) marker
','pixel marker
'1'tri_down marker
'2'tri_up marker
'3'tri_left marker
'4'tri_right marker
'8'octagon marker
'p'pentagon marker
'P'plus (filled) marker
'h'hexagon1 marker
'H'hexagon2 marker
'D'diamond marker
'd'thin_diamond marker

ls线条形状

characterdescription
'-'实线
'--'虚线
'-.'虚线加点
':'点线

lw线条宽度

浮点数,如:lw=2/3/1.5

c颜色

characterdescription
'b'蓝色
'g'绿色
'r'红色
'c'青色
'y'黄色
'k'黑色
'w'白色
'm'洋红色

一个小例子:

import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

结果为:

在这里插入图片描述

To be continue
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值