栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 数据分析——Matplotlib相关知识

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 数据分析——Matplotlib相关知识

Python 数据分析——Matplotlib相关知识

第二章 Matplotlib相关知识 —— 绘图 (二)


文章目录
  • Python 数据分析——Matplotlib相关知识
  • 前言
  • 一、Figure容器
    • 1.常见属性
    • 2. 添加或修改元素
  • 二、Axes容器
    • 常见属性
  • 三、Axis容器
  • 四、Tick容器
    • 常见属性
  • 参考


前言

上篇主要介绍了Artist的两种类型:primitives 和containers中的primitives 类型,这次主要是介绍containers 类型


Object container容器会包含一些primitives,并且容器还有它自身的属性。
比如Axes Artist,它是一种容器,它包含了很多primitives,比如Line2D,Text;同时,它也有自身的属性,比如xscal,用来控制X轴是linear还是log的。

一、Figure容器 1.常见属性
Figure attribute(属性)Description(描述)
axes一个Axes实例的列表(包括Subplot)
patchFigure的背景矩形
images一个FigureImages patch列表
legends一个Figure Legend实例列表(不同于Axes.legends)
lines一个Line2D实例的列表
texts一个Figure Text实例列表
patchesPatch的列表

matplotlib.figure.Figure是Artist最顶层的container对象容器,它包含了图表中的所有元素。一张图表的背景就是在Figure.patch的一个矩形Rectangle。
当我们向图表添加Figure.add_subplot()或者Figure.add_axes()元素时,这些都会被添加到Figure.axes列表中。

  • 示例
fig = plt.figure()
ax1 = fig.add_subplot(211) # 作一幅2*1的图,选择第1个子图
# 也可以写作 ax1 = fig.add_subplot(2,1,1)

ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.3]) # 位置参数,四个数分别代表了(left,bottom,width,height)
print(ax1) 
print(fig.axes) # fig.axes 中包含了subplot和axes两个实例


2. 添加或修改元素

通过Figure.add_subplot()、Figure.add_axes()来添加元素,通过Figure.delaxes()来删除元素。

  • 可以迭代或者访问Figure.axes中的Axes,然后修改这个Axes的属性。
  • 比如下面的遍历axes里的内容,并且添加网格线:
fig = plt.figure()
ax1 = fig.add_subplot(211)

for ax in fig.axes:
    ax.grid(True)

  • 皮一下
fig, axs = plt.subplots(3, 1, figsize=(5, 6), tight_layout=True)
row = [1,2,3]
for i,ax in list(zip(row,fig.axes)):
    if i == 0:
        ax.grid(True)
    elif i == 1:
        ax.xaxis.grid()
    else:
        ax.yaxis.grid() 


二、Axes容器 常见属性
Axes attribute(属性)Description(描述)
artistsArtist实例列表
patchAxes所在的矩形实例
collectionsCollection实例
imagesAxes图像
legendsLegend 实例
lines一个Line2D实例
texts一个Figure Text实例
xaxismatplotlib.axis.XAxis 实例
yaxismatplotlib.axis.YAxis 实例

  • 示例
fig = plt.figure()
ax = fig.add_subplot(111)
rect = ax.patch  # axes的patch是一个Rectangle实例
rect.set_facecolor('green')


三、Axis容器
  • 官网实例
# plt.figure creates a matplotlib.figure.Figure instance
fig = plt.figure()
rect = fig.patch  # a rectangle instance
rect.set_facecolor('lightgoldenrodyellow')

ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4])
rect = ax1.patch
rect.set_facecolor('lightslategray')


for label in ax1.xaxis.get_ticklabels():
    # label is a Text instance
    label.set_color('red')
    label.set_rotation(45)
    label.set_fontsize(16)

for line in ax1.yaxis.get_ticklines():
    # line is a Line2D instance
    line.set_color('green')
    line.set_markersize(25)
    line.set_markeredgewidth(3)

plt.show()

四、Tick容器

matplotlib.axis.Tick是从Figure到Axes到Axis到Tick中最末端的容器对象。
Tick包含了tick、grid line实例以及对应的label。

常见属性
Tick attribute(属性)Description(描述)
Tick.tick1lineLine2D实例
Tick.tick2lineLine2D实例
Tick.gridlineLine2D实例
Tick.label1Text实例
Tick.label2Text实例
  • 示例
import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)

fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))

# Use automatic StrMethodFormatter
ax.yaxis.set_major_formatter('${x:1.2f}')

ax.yaxis.set_tick_params(which='major', labelcolor='green',
                         labelleft=False, labelright=True)

plt.show()


参考

1.Datawhale数据可视化小组开源项目:《Fantastic-Matplotlib》
2.Matplotlib官方文档

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号