前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Figure解析

Figure解析

作者头像
陆勤_数据人网
发布2020-04-07 12:12:14
8120
发布2020-04-07 12:12:14
举报

“PDFMV框架是问题-数据-特征-模型-价值五个英文字母的首字母组合而成,它是以问题为导向,数据为驱动,利用特征和模型从数据中学习到知识,以创造价值的系统化过程。”

1 图形解析

一个图形包括哪些元素呢?图1,图形的解析,展示构成一个Figure的一些matplotlib元素名称。我们可以使用matplotlib库实现对各个元素的设计和管理

图1 一个Figure的解析

2 matplot库画图的各个元素

  • 1:Figure Figure包括图形的所有元素,这些图形的元素都可以做定制化。
  • 2:Axes Axes是figure的sub-section。一个figure可以包括多个Axes,每个Axes可以表示多个graphs。图1中,一个Axes,包括两个线图。
  • 3:Axis Axis是axes的一个元素,二维空间有x轴和y轴,三维空间有x轴,y轴和z轴。
  • 4:Label 标记,用于对坐标轴,图形进行解释,让图形的意思更明了。
  • 5:Legend 图例,当一个坐标系统有多幅图,图1中,有两个直线图,每个线图有它们自己的标记和表示,把所有标记放在一块称之为图例,用来增加可视化清晰特性。
  • 6:Title 图标题,每一个坐标轴系统都会有一个标题,增强图形可读性,告诉这个图是用来做什么。
  • 7:Ticklabels 坐标轴的刻度以及刻度标签,刻度有粗分和细分,前者是major ticks和minor ticks,这些刻度的命名,称之为major ticklabels和minor ticklabels。
  • 8:Spines 图形的边界线,每个axes包括4个边界线,分别是(上、下,左,右)。
  • 9:Grid 图形的网格线,为了增强图形的可读性,在图1中,虚线是网格线。

3 图形解剖绘制参考代码

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter

np.random.seed(19680801)

X = np.linspace(0.5, 3.5, 100)
Y1 = 3+np.cos(X)
Y2 = 1+np.cos(1+X/0.75)/2
Y3 = np.random.uniform(Y1, Y2, len(X))

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1, aspect=1)


def minor_tick(x, pos):
    if not x % 1.0:
        return ""
    return "%.2f" % x

ax.xaxis.set_major_locator(MultipleLocator(1.000))
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_major_locator(MultipleLocator(1.000))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
ax.xaxis.set_minor_formatter(FuncFormatter(minor_tick))

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

ax.tick_params(which='major', width=1.0)
ax.tick_params(which='major', length=10)
ax.tick_params(which='minor', width=1.0, labelsize=10)
ax.tick_params(which='minor', length=5, labelsize=10, labelcolor='0.25')

ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)

ax.plot(X, Y1, c=(0.25, 0.25, 1.00), lw=2, label="Blue signal", zorder=10)
ax.plot(X, Y2, c=(1.00, 0.25, 0.25), lw=2, label="Red signal")
ax.plot(X, Y3, linewidth=0,
        marker='o', markerfacecolor='w', markeredgecolor='k')

ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
ax.set_xlabel("X axis label")
ax.set_ylabel("Y axis label")

ax.legend()


def circle(x, y, radius=0.15):
    from matplotlib.patches import Circle
    from matplotlib.patheffects import withStroke
    circle = Circle((x, y), radius, clip_on=False, zorder=10, linewidth=1,
                    edgecolor='black', facecolor=(0, 0, 0, .0125),
                    path_effects=[withStroke(linewidth=5, foreground='w')])
    ax.add_artist(circle)


def text(x, y, text):
    ax.text(x, y, text, backgroundcolor="white",
            ha='center', va='top', weight='bold', color='blue')


# Minor tick
circle(0.50, -0.10)
text(0.50, -0.32, "Minor tick label")

# Major tick
circle(-0.03, 4.00)
text(0.03, 3.80, "Major tick")

# Minor tick
circle(0.00, 3.50)
text(0.00, 3.30, "Minor tick")

# Major tick label
circle(-0.15, 3.00)
text(-0.15, 2.80, "Major tick label")

# X Label
circle(1.80, -0.27)
text(1.80, -0.45, "X axis label")

# Y Label
circle(-0.27, 1.80)
text(-0.27, 1.6, "Y axis label")

# Title
circle(1.60, 4.13)
text(1.60, 3.93, "Title")

# Blue plot
circle(1.75, 2.80)
text(1.75, 2.60, "Line\n(line plot)")

# Red plot
circle(1.20, 0.60)
text(1.20, 0.40, "Line\n(line plot)")

# Scatter plot
circle(3.20, 1.75)
text(3.20, 1.55, "Markers\n(scatter plot)")

# Grid
circle(3.00, 3.00)
text(3.00, 2.80, "Grid")

# Legend
circle(3.70, 3.80)
text(3.70, 3.60, "Legend")

# Axes
circle(0.5, 0.5)
text(0.5, 0.3, "Axes")

# Figure
circle(-0.3, 0.65)
text(-0.3, 0.45, "Figure")

color = 'blue'
ax.annotate('Spines', xy=(4.0, 0.35), xytext=(3.3, 0.5),
            weight='bold', color=color,
            arrowprops=dict(arrowstyle='->',
                            connectionstyle="arc3",
                            color=color))

ax.annotate('', xy=(3.15, 0.0), xytext=(3.45, 0.45),
            weight='bold', color=color,
            arrowprops=dict(arrowstyle='->',
                            connectionstyle="arc3",
                            color=color))

ax.text(4.0, -0.4, "Made with http://matplotlib.org",
        fontsize=10, ha="right", color='.5')

plt.show()

代码来源:https://matplotlib.org/3.2.1/gallery/showcase/anatomy.html

为什么我们要从宏观上面了解和认识图形的各个元素呢?

  1. 这是让我们知道一个Figure包括哪些元素,这些元素分别是什么以及有什么作用,彼此之间的关系层次是什么。
  2. 这是让我们清楚一个Figure要做定制化,可以从哪些细节入手。

因此,做Figure之前,先想想它有哪些组件,这些组件怎么设计和实现,根据现有的数据和探索的关系,需要选择什么样的Grpah

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据科学与人工智能 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 图形解析
  • 2 matplot库画图的各个元素
  • 3 图形解剖绘制参考代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档