本文是官方教程The Lifecycle of a Plot的翻译
绘制图表的流程
(The Lifecycle of a Plot)
本教程旨在展示使用Matplotlib绘制单一图表的全过程。教程从原始数据入手,最后绘制成自定义的图表。整个过程中教程会试图将matplotlib的特性与最佳实践相结合。
注释:
本教程是基于Chris Moffitt的博客由Chris Holdgraf改变而来。
关于 OO API 与 Pyplot
(A note on the Object-Oriented API vs Pyplot)
matplotlib有两种界面,本教程使用面向对象的OO接口。本教程中,使用axes.Axes实例渲染figure.Figure实例的可视化效果。
另外一种接口是基于MATLAB的状态机接口,使用pyplot模块。
总而言之,记得如下事项:
-Figure是最终的图像,可能会包含一个或以上的Axes
-Axes在图表中是一个独立的“图”(plot),但是要搞清楚axes和axis的区别
数据准备
本教程使用如下数据,包含几家公司的销售信息。
# sphinx_gallery_thumbnail_number = 10
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
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)
开始 Getting started
实验数据很明显可以作为柱状图来显示。我们用OO模式来完成,首先需要生成figure.Figure和axes.Axes实例。Figure类似一个画布,Axes是画布上的一部分,其实也就是大图上的一块小图。
注意:
Figure可以有多个axes。参考Tight Layout tutorial。
fig, ax = plt.subplots()
好,现在我们已经有了一张空白的图了,下面将会在上面进行绘图。
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
控制样式 Controlling the style
matplotlib提供很多样式可供使用,可以使用如下模块查看样式列表
pyplot.style
>>>print(plt.style.available)
out:['seaborn-ticks', 'ggplot', 'dark_background', 'bmh', 'seaborn-poster', 'seaborn-notebook', 'fast', 'seaborn', 'classic', 'Solarize_Light2', 'seaborn-dark', 'seaborn-pastel', 'seaborn-muted', '_classic_test', 'seaborn-paper', 'seaborn-colorblind', 'seaborn-bright', 'seaborn-talk', 'seaborn-dark-palette', 'tableau-colorblind10', 'seaborn-darkgrid', 'seaborn-whitegrid', 'fivethirtyeight', 'grayscale', 'seaborn-white', 'seaborn-deep']
激活样式使用如下命令:
plt.style.use('fivethirtyeight')
重写上面的绘图代码:
fig, ax = plt.subplots()
ax.barh(group_name, group_data)
样式控制很多方面,比如颜色、线宽、背景等等。
自定义绘图 Customizing the plot
目前我们已经绘制了一张图,接下来我们将调整这张图以便于打印。首先,旋转x轴(x-axis)让它更清楚的显示,可以通过axes.Axes.get_xticklabels()方法获得标签。
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
对于一次性设置多个参数的情况,可以使用pyplot.setp()函数,一次性传递列表(或者多个列表)给matplotlib对象。
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
图片看起来是被裁切了,可以用rcParams中的autolayout参数进行调节。详细内容可参阅Customizing Matplotlib with style sheets and rcParams
plt.rcParams.update({'figure.autolayout': True})
fig, ax = plt.subplots()
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
下一步给图表增加标签。通过OO方式添加,使用axes.Axes.set()方法添加到轴域(Axes)对象。
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')
也可以使用plt.subplots()调整图表(plot)的尺寸,这里要用到figsize关键字参数(kwarg)。
注意:
虽然NumPy中的索引遵循形式(行,列),但figsize kwarg遵循形式(宽度,高度)。 这遵循可视化中的惯例,遗憾的是它们与线性代数的惯例不同。
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类以函数的形式指定自定义格式指南。 下面我们将定义一个以整数作为输入的函数,并返回一个字符串作为输出。
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)
接下来我们可以把这个自定义函数应用于绘图之中。使用轴的xaxis属性完成这一动作。这使得我们可以在图表的指定轴上完成相应动作。
fig, ax = plt.subplots(figsize=(6, 8))
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)
合并多个可视化效果
matplotlib可以在同一个axes.Axes实例上绘制多个元素,只需要在axes对象上调用另一个绘图方法。
fig, ax = plt.subplots(figsize=(8, 8))
ax.barh(group_names, group_data)
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
# Add a vertical line, here we set the style in the function call
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])
fig.subplots_adjust(right=.1)
plt.show()
保存图表 Saving our plot
matplotlib 提供了多种保存文件的格式,参照如下命令print(fig.canvas.get_supported_filetypes())
{'ps': 'Postscript', 'eps': 'Encapsulated Postscript', 'pdf': 'Portable Document Format', 'pgf': 'PGF code for LaTeX', 'png': 'Portable Network Graphics', 'raw': 'Raw RGBA bitmap', 'rgba': 'Raw RGBA bitmap', 'svg': 'Scalable Vector Graphics', 'svgz': 'Scalable Vector Graphics', 'jpg': 'Joint Photographic Experts Group', 'jpeg': 'Joint Photographic Experts Group', 'tif': 'Tagged Image File Format', 'tiff': 'Tagged Image File Format'}
使用figure.Figure.savefig()保存图表,注意如下参数:
transparent=True makes the background of the saved figure transparent if the format supports it.
dpi=80 controls the resolution (dots per square inch) of the output.
bbox_inches="tight" fits the bounds of the figure to our plot.
fig.savefig('sales.png', transparent=False, dpi=100, bbox_inches='tight')
The END.
转载于:https://www.jianshu.com/p/8c6c9a438646