美文网首页生物信息学生物信息学与算法生物信息学习
matplotlib绘图基础(3)—— 一张图的图生历程

matplotlib绘图基础(3)—— 一张图的图生历程

作者: WooWoods | 来源:发表于2019-10-07 22:44 被阅读0次

今天我们来了解一下一张图的生命周期,每张图的可视化过程中都经历了什么,以便更深入地理解matplotlib。

Object-Oriented API vs Pyplot

matplotlib有两种接口,第一种是面向对象的接口,它是利用一个figure.Figure实例上的axes.Axes实例来渲染图像的。
第二种是MATLAB风格的接口,被封装在pyplot模块当中。
重点关注两点:

  • Figure是最终的图像,其上包含1个或多个Axes
  • Axes代表一个独立的图(plot),主要不要将其与“axis”混淆,后者指的是一个plot的x/y坐标轴

尽量使用面向对象的接口而不是pyplot接口,这样我们可以调用直接从Axes类画图的方法,它在我们定制化图形时能够赋予我们更多的灵活性。

准备数据

data = {'Barton LLC': 109438.50,
        'Frami, Hills and Schmidt': 103569.59,
        'Fritsch, Russel and Anderson': 112214.71,
        'Jerde-Hilpert': 112591.43,
        'Keeling LLC': 100934.30,
        'Koepp Ltd': 103660.54,
        'Kulas Inc': 137351.96,
        'Trantow-Barrows': 123381.38,
        'White-Trantow': 135841.99,
        'Will LLC': 104437.60}
group_data = list(data.values())
group_names = list(data.keys())
group_mean = np.mean(group_data)

这是一组多家企业的销售信息数据,适合用来画柱形图。
我们要使用面向对象的接口来画图,首先生成一个figure.Figure和axes.Axes的实例,Figure就相当于一张画布,而Axes就是画布当中的一块区域,我们在其中渲染特定的图形。

fig, ax = plt.subplots()

有了Axes实例后,我们就可以在上面画图了。

ax.barh(group_names, group_data)

风格控制

matplotlib中有多种风格

print(plt.style.available)
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']

通过如下命令激活风格

plt.style.use('fivethirtyeight')

和之前的图比较一下,


定制图形

现在我们有了一般化的图形,我们可以在此基础上进行微调,使其达到我们想要的效果。首先将x-axis的label进行旋转。

fig, ax = plt.subplots()
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')

接下来,为图像加标题

ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
       title='Company Revenue')

我们还可以通过pyplot.subplots()函数调整图像大小。

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
       title='Company Revenue')

我们可以通过 ticker.FuncFormatter类自定义label 的格式。下面的例子中定义了一个函数,该函数接受一个整数,返回一个字符串:

def currency(x, pos):
    """The two args are the value and tick position"""
    if x >= 1e6:
        s = '${:1.1f}M'.format(x*1e-6)
    else:
        s = '${:1.0f}K'.format(x*1e-3)
    return s

formatter = FuncFormatter(currency)

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
       title='Company Revenue')
ax.xaxis.set_major_formatter(formatter)

组合图形

可以在一个Axes实例上渲染不同的图形。

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')


ax.axvline(group_mean, ls='--', color='r')

# Annotate new companies
for group in [3, 5, 8]:
    ax.text(145000, group, "New Company", fontsize=10,
            verticalalignment="center")

# Now we'll move our title up since it's getting a little cramped
ax.title.set(y=1.05)
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company',
       title='Company Revenue')
ax.xaxis.set_major_formatter(formatter)
ax.set_xticks([0, 25e3, 50e3, 75e3, 100e3, 125e3])

plt.show()

保存图片

fig.savefig('sales.png', transparent=False, dpi=80, bbox_inches="tight")

相关文章

网友评论

    本文标题:matplotlib绘图基础(3)—— 一张图的图生历程

    本文链接:https://www.haomeiwen.com/subject/yxfvpctx.html