数据可视化matplotlib

什么是 matplotlib

Matplotlib 可能是 Python 2D-绘图领域使用最广泛的套件。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。这里将会探索 matplotlib 的常见用法。

菜鸟教程

使用

Ⅰ. 绘制折线图

绘制简单图
from matplotlib import pyplot as plt
x = range(2, 26, 2)
y = [1, 20, 34, 4, 53, 64, 7, 8, 94, -10, 11, 22]
plt.plot(x, y)
plt.show()
修改图片大小
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
  • num 图像编号或名称,数字表示编号,字符串表示名称。
  • figsize 图像大小,宽高,单位为英寸。
  • dpi 图像的分辨率,每英寸多少个像素。
  • facecolor 背景颜色。
  • edgecolor 边框颜色。
  • frameon 是否显示边框。

用例:

plt.figure(figsize=(20, 8), dpi=80)``figsize

保存图片
plt.savefig("./xxx.svg")

保存图片到本地,支持 svg 矢量图形。

设置坐标轴

以 x 轴为例。

x_ticks = [i/2 for i in range(4, 49)]
plt.xticks(x_ticks[::3])

从 2 开始,每隔 1.5 个单位显示一个刻度。

还可以起别名。

x_ticks = [i/2 for i in range(4, 49)]
x_labels = ["h" + str(i) for i in range(1, 14)]
plt.xticks(x_ticks[::3], x_labels)

字体旋转 rotation 后接旋转度数。

plt.xticks(list(range(120))[::3], labels=x[::3], rotation=45)

y 轴即 plt.yticks

plt.yticks(range(min(y), max(y) + 1))
设置字体

查看 linux 有哪些字体: fc-list 有哪些中文字体 fc-list :lang=zh

import matplotlib
font = {"family": "Noto Sans Mono",
        "weight": "bold",
        "size": "larger"
        }
matplotlib.rc("font", **font)

或者:

from matplotlib import font_manager

my_font = font_manager.FontProperties(
    fname="/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc")

plt.xticks(list(range(120))[::3], labels=x[::3], rotation=45,
           fontproperties=my_font)
  • family: A list of font names in decreasing order of priority.
    The items may include a generic font family name, either
    ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, or ‘monospace’.
    In that case, the actual font to be used will be looked up
    from the associated rcParam.

  • style: Either ‘normal’, ‘italic’ or ‘oblique’.

  • variant: Either ‘normal’ or ‘small-caps’.

  • stretch: A numeric value in the range 0-1000 or one of
    ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’,
    ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’,
    ‘extra-expanded’ or ‘ultra-expanded’

  • weight: A numeric value in the range 0-1000 or one of
    ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’,
    ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’,
    ‘extra bold’, ‘black’

  • size: Either an relative value of ‘xx-small’, ‘x-small’,
    ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’ or an
    absolute font size, e.g., 12

添加描述信息
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度 单位(摄氏度)", fontproperties=my_font)
plt.title("10~12点每分钟气温变化情况", fontproperties=my_font)
绘制网格
plt.grid()
  • alpha 设置透明度。范围 0~1
  • axis 显示哪个轴的网格 axis='x' 显示 x 轴,axis='y 显示 y 轴,axis='both 都显示。
  • color 设置网格线颜色。
  • linestyle 线条风格,plt.grid(linestyle='-.')
绘制多次图形
plt.plot(x, y1, color="green", label="XX1")
plt.plot(x, y2, color="blue", label="XX2")
plt.legend(prop=my_font)

多次调用 plot 方法即可,为了区分,给 plot 方法传入 label 参数,最后调用 legend 方法显示即可。

Ⅱ. 绘制散点图

跟绘制折线图没太大却别。

plt.plot() 改成 plt.scatter() 即可。

隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)

Ⅲ. 绘制条形图

方法 plt.bar() 绘制竖直条形图,方法 plt.barh() 绘制横向条形图。

plt.bar(x, y width=bar_width, color="blue", label="xxx"
plt.barh(x, y height=bar_width, color="red", label="xxx"

Ⅳ. 绘制直方图

直方图与条形图区别
  1. 条形图 是用条形的 “长度” 表示各类别频数的多少,其宽度(表示类别)则是固定的。

  2. 直方图 是用 “面积” 表示各组频数的多少,矩形的高度表示每一组的频数或频率,宽度则表示各组的组距,因此其高度与宽度均有意义。$ 组数 = \frac{极差}{组距}$

  3. 条形图主要用于展示分类数据,而直方图则主要用于展示数据型数据。’

频数分布直方图
d = 3
num_bins = (max(a) - min(a)) // d
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(a, num_bins)
plt.xticks(range(min(a), max(a) + d, d))
plt.grid()
plt.show()
频率分布直方图
d = 3
num_bins = (max(a) - min(a)) // d
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(a, num_bins, density=True)
plt.xticks(range(min(a), max(a) + d, d))
plt.grid()
plt.show()

总结

这个内容太多了,只罗列了一些比较常用的。更多的可以去看源码。
更多图形直接参考 matplotlib 官方文档

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值