Matplotlib是Python中风行的数据可视化库,仅应用简略的几行代码就能够生成图表。然而默认的办法是生成的图表很简略,如果想加强数据演示的影响和清晰度,能够试试本文总结的10个高级技巧,这些技巧能够将可视化晋升到一个新的程度:

1、rcParams

rcParams字典。它蕴含了用于创立图形的默认款式的所有Matplotlib设置。你能够间接从matplotlib命名空间导入它:

 from matplotlib import rcParams >>> rcParams ... 'axes.grid': False, 'axes.grid.axis': 'both', 'axes.grid.which': 'major', 'axes.labelcolor': 'black', 'axes.labelpad': 4.0, 'axes.labelsize': 'medium', 'axes.labelweight': 'normal', 'axes.linewidth': 0.8, ...  rcParams['figure.figsize'] = 8, 6 rcParams['legend.fontsize'] = "large" rcParams['xtick.major.size'] = 4 rcParams['xtick.minor.size'] = 1

这时所有的Matplotlib设置,如果你想批改任何的Matplotlib参数,间接批改这个字典就能够了,你甚至能够将他序列化到本地,而后在其余我的项目中间接加载,这样你的每一个Matplotlib实例应用的都是雷同的配置了。

还能够调用PyPlot的rcdefaults函数,它会将所有参数重置成默认值。

 plt.rcdefaults()

2、get_* functions

在底层,Matplotlib是齐全面向对象的。

上图中看到的每个独自的组件都是作为一个独自的类实现的。它们都继承自基类Matplotlib Artist。

然而类太多,并且每个类的参数都不一样这会给应用带来很大的不不便,所以Matplotlib定制了有许多以get_前缀结尾的函数,能够间接创立图形中的组件。上面是一个例子:

 fig, ax = plt.subplots()  >>> [func for func in dir(ax) if func.startswith("get")]  ['get_adjustable',  'get_label',  'get_legend',  'get_legend_handles_labels',  'get_lines',  'get_navigate',  'get_title',  'get_transform',  'get_xmajorticklabels',  'get_xminorticklabels',  'get_xscale',  'get_xticklabels',  'get_zorder']

假如咱们想自定义一个图形的坐标:

 x = np.linspace(0, 2, 100)  fig, ax = plt.subplots()  # Create a figure and an axes.  l1 = ax.plot(x, x, label="linear") l2 = ax.plot(x, x ** 2, label="quadratic") l3 = ax.plot(x, x ** 3, label="cubic")  ax.set_title("Simple Plot")  plt.show()

这很简略,只需在axes对象上调用get_xticklabels,就能够失去Matplotlib Text实例的列表:

 >>> ax.get_xticklabels()  [Text(0, 0, 'Ideal'),  Text(1, 0, 'Premium'),  Text(2, 0, 'Very Good'),  Text(3, 0, 'Good'),  Text(4, 0, 'Fair')]

还能够应用get_xticklines调整刻度线,或者应用get_xticks调整刻度的地位。

曾经取得了对象,上面就能够进行调整了

3、get / setp

调用plt.getp函数,能够查看它以后具备的参数。例如,假如咱们想要款式化上面图的l2:

 x = np.linspace(0, 2, 100)  fig, ax = plt.subplots()  # Create a figure and an axes.  l1 = ax.plot(x, x, label="linear") l2 = ax.plot(x, x ** 2, label="quadratic") l3 = ax.plot(x, x ** 3, label="cubic")  ax.set_title("Simple Plot")  plt.show()

这个办法返回了图表的所有属性

 >>> plt.getp(l2)     ...     drawstyle or ds = default     figure = Figure(640x480)     linestyle or ls = -     linewidth or lw = 1.5     marker = None     markeredgecolor or mec = #ff7f0e     markeredgewidth or mew = 1.0     markerfacecolor or mfc = #ff7f0e     markerfacecoloralt or mfcalt = none     zorder = 2     ...

而plt.setp能够更改属性在没有任何参数的对象上调用this会打印出该对象能够承受的属性值:

 >>> plt.setp(l2)   ...   linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}   linewidth or lw: float   sketch_params: (scale: float, length: float, randomness: float)   snap: bool or None   zorder: float   ...

要打印单个属性的可能值,能够将属性的名称作为字符串输出setp:

 >>> plt.setp(l2, "linestyle") linestyle: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}

批改属性的办法如下:

 >>> plt.setp(l2, linestyle="-.", lw=5, color="red", alpha=0.5) [None, None, None, None]

要查看更改后的以后图形,只需在图形对象上调用get_figure:

 fig.get_figure()

第二行的款式曾经变了

4、Legends

Legends能够不便的通知咱们图中每个组件的含意,默认是这样显示的:

 x = np.linspace(0, 2, 100)  fig, ax = plt.subplots()  # Create a figure and an axes.  l1 = ax.plot(x, x, label="linear") l2 = ax.plot(x, x ** 2, label="quadratic") l3 = ax.plot(x, x ** 3, label="cubic")  ax.set_title("Simple Plot")  ax.legend()  plt.show()

咱们能够调整他的参数,例如:

图例的地位、字体属性、大小,色彩,款式、图例中的列数,等等

能够在创立前设置,也能够在创立后应用get_legend提取,并应用getp、setp函数。

5、cycler

你有没有想过Matplotlib是如何本人扭转色彩或循环不同格调的?

在底层,Matplotlib应用名为Cyclers的Python内置对象:

 from cycler import cycler  c1 = cycler(arg1=[1, 2, 3, 4]) >>> c1

这个循环函数承受任何键值参数并创立一个字典列表:

 c2 = cycler(arg2=list("rgba"))  for i in c2:     print(i)  ------------------------------  {'arg2': 'r'} {'arg2': 'g'} {'arg2': 'b'} {'arg2': 'a'}

还能够将多个循环器与“plus”和“multiply”操作符组合起来,这样能够取得索引到索引或穷举的参数组合:

 for i in c1 + c2:     print(i)  --------------------------------  {'arg1': 1, 'arg2': 'r'} {'arg1': 2, 'arg2': 'g'} {'arg1': 3, 'arg2': 'b'} {'arg1': 4, 'arg2': 'a'}

将这个自定义循环器并将其传递给Matplotlib,就能够定制款式。上面,咱们创立四种不同的线条款式,容许Matplotlib循环应用不同的线条色彩,款式和大小:

 line_prop_cycler = (     cycler(color=list("rgcy"))     + cycler(ls=["-", "--", "-.", ":"])     + cycler(lw=[3, 6, 9, 12]) )

能够应用axes对象的set_prop_cycle函数将这个自定义循环器传递给绘图:

 x = np.linspace(0, 2 * np.pi, 50) offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False) yy = np.transpose([np.sin(x + phi) for phi in offsets])  fig, ax = plt.subplots(figsize=(8, 4))  ax.set_prop_cycle(line_prop_cycler)  # Set propcycle before plotting ax.plot(x, yy)  plt.show();

rcParams字典中默认设置如下:

 rcParams["axes.prop_cycle"]

咱们能够间接批改

6、tick_params

轴刻度应该精确地传播数据点及其单位的最小值和最大值,并显示几个要害的检查点,以便在不同的绘图局部之间进行比拟。

大多数tick属性能够应用axes对象的tick_params函数来管制。以下是文档中的例子:

 >>> ax.tick_params()  Parameters ---------- axis : {'x', 'y', 'both'}, default: 'both'     The axis to which the parameters are applied. which : {'major', 'minor', 'both'}, default: 'major'     The group of ticks to which the parameters are applied. reset : bool, default: False     Whether to reset the ticks to defaults before updating them.  Other Parameters ---------------- direction : {'in', 'out', 'inout'}     Puts ticks inside the axes, outside the axes, or both. length : float     Tick length in points. width : float     Tick width in points. color : color     Tick color.

首先应该指定的两个参数是axis和which。这些参数将利用于X或Y轴刻度,以及最小和最大刻度。

大多数时候,在Matplotlib中不会看到小刻度。如果须要能够应用axes对象上的minortics_on函数:

 fig, ax = plt.subplots(figsize=(3, 2))  >>> ax.minorticks_on()

7、Tickers

如果不像自定义tick参数(因为很麻烦)。能够应用许多内置的Matplotlib的“主题”汇合(称为tickers)。

 from matplotlib import ticker dir(ticker) ['AutoLocator',  'AutoMinorLocator',  'EngFormatter',  'FixedFormatter',  'FixedLocator',  'FormatStrFormatter',  'Formatter',  'FuncFormatter',  'IndexFormatter',  'IndexLocator',  'Integral',  'LinearLocator', ]

在ticker模块下有许多这样的子模块。个别状况下题目中带有Locator的控件管制刻度的地位。而Formatters 则示意标签的款式。抉择好后能够应用上面的形式进行设置:

 from matplotlib.ticker import EngFormatter  ax.xaxis.set_major_formatter(EngFormatter())

应用axes对象的xaxis或yaxis属性,调用set_major(minor)_formatter(locator)函数,并传入类名。

8、grid

自定义网格线能够突出数据范畴。在Matplotlib中,能够应用轴线对象的网格函数创立和自定义网格。上面是一个垂直网格的例子:

 fig, ax = plt.subplots()  ax.grid(axis="x", linestyle=":", lw=3, color="r")

9、bar_label

条形图在数据分析中很常见。它们最重要的中央就是每个条的高度,条形标签能够突出每个条的显示。

bar_label函数承受一个BarContainer对象作为参数,并主动标注每个bar的高度。

上面是Seaborn的一个简略的计数图:

 import seaborn as sns  diamonds = sns.load_dataset("diamonds")  ax = sns.countplot(diamonds["cut"])

每次应用Seaborn或ax.bar等函数创立barplot时,BarContainer对象都会被增加到图中。能够应用axes对象的containers属性来检索这个容器对象:

 ax.containers [<BarContainer object of 5 artists>]

在下面的列表中有一个BarContainer对象有5个bar。咱们只需在创立了plot之后将这个对象传递给bar_label:

 ax = sns.countplot(diamonds["cut"]) ax.bar_label(ax.containers[0], padding=1) ax.set_ylim(0, 25000) plt.show();

10、zorder

当有很多图的时候,显示程序是十分重要的。你须要确保在画布上以适当的程序绘制每个图形,就须要zorder参数。

上面,咱们用不同的zorders创立了三行:

 x = np.linspace(0, 7.5, 100)  plt.plot(x, np.sin(x), label="zorder=2", zorder=2)  # bottom plt.plot(x, np.sin(x + 0.5), label="zorder=3", zorder=3) plt.axhline(0, label="zorder=2.5", color="lightgrey", zorder=2.5)  plt.title("Custom order of elements")  l = plt.legend(loc="upper right") l.set_zorder(2.5)  # legend between blue and orange line  plt.show()

能够看到zorder越大,就会在最上方显示,笼罩掉小的组件。

总结

Matplotlib在2023年6月的下载量超过3000万,简直是其最大竞争对手Plotly的4倍。Matplotlib的胜利不仅仅在于它的简略(只须要几行代码就能生成简略的图形),还在于他的功能强大,然而要应用这些弱小的性能就须要应用他的高级性能,然而这些高级性能往往须要比较复杂的配置或者参数,须要咱们浏览官网的文档。所以才呈现了seaborn,他将Matplotlib进行了整合不仅简略而且难看。

然而有时咱们须要更深刻的定制性能,seaborn兴许还达不到咱们的指标,咱们只能本人定义的参数,本文总结的是个高级技巧能够轻松的帮你残缺自定义Matplotlib的工作。

https://avoid.overfit.cn/post/fece2cde8dbd4f899de00f5509385c6c

作者:Bex T