如何在 Python 上制作简单高效的绘图 [英] how to make easy and efficient plots on Python

查看:41
本文介绍了如何在 Python 上制作简单高效的绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绘图中使用 matplotlib,我觉得它很棒,但有时太复杂了.举个例子:

I use matplotlib for my plots, I find it great, but sometimes too much complicated. Here an example:

import matplotlib.pyplot as plt
import numpy as np

idx1 = -3
idx2 = 3

x = np.arange(-3, 3, 0.01)
y = np.sin(np.pi*x*7)/(np.pi*x*7)

major_ticks = np.arange(idx1, idx2, 1)
minor_ticks = np.arange(idx1, idx2, 0.1)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylim(-0.3, 1.2)
ax.set_xlim(idx1, idx2)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor = True)
ax.grid(True, which = 'both')
ax.tick_params(axis = 'x', labelsize = 18)
ax.tick_params(axis = 'y', labelsize = 18)

ax.plot(x, y)
plt.show()

在 matplotlib 和/或 seaborn 上是否有任何实现,我可以在其中提供所有这些绘图设置,仅作为函数的参数?它可能会大大减少代码行数并使脚本更易于编写和理解.

Is there anything implemented on matplotlib and/or seaborn in which I can provide all these plot settings just as argument of a function only? It may considerably reduce the number of code lines and make the script easier both to write and understand.

推荐答案

Matplotlib 提供了一个面向对象的 API.这意味着图形的所有元素实际上都是可以获取和设置属性并且可以轻松操作的对象.这使得 matplotlib 非常灵活,几乎可以生成您想象的任何绘图.

Matplotlib provides an object oriented API. This means that all the elements of the figure are acutally objects for which one can get and set properties and which can be easily manipulated. This makes matplotlib really flexible such that it can produce almost any plot you'd imagine.

由于图可能包含一百个或更多元素,因此具有相同灵活性的函数将需要大量可能的参数.记住一个函数的所有可能参数并不一定比记住一个类的所有可能属性更容易.

Since a plot may consist of a hundred or more elements, a function that would allow the same flexibility would need that amount of possible arguments. It is not necessarily easier to remember all possible arguments of a function than all possible attributes of a class.

有一个函数调用完成所有这些,并不一定意味着您必须输入更少的字符.命令的顺序不同.

Having a single function call that does all of this, does not necessarily mean that you have to type in less characters. The commands would just be ordered differently.

此外,面向对象的方法允许将事情分开.轴的某些属性,例如网格或轴标签,完全独立于您绘制到轴上的内容.因此,您不希望在对 plot 的调用中设置 xticks,因为它们根本不相关,并且在同一轴上绘制两条线时设置两次相同的刻度标签可能会非常混乱.

Furthermore the object oriented approach allows to keep things seperate. Some properties of the axes, like the grid or the axis labels are completely independend on what you plot to the axes. So you wouldn't want to set the xticks in the call to plot, because they are simply not related and it may be very confusing to set twice the same ticklabels when plotting two lines in the same axes.

另一方面,matplotlib 非常简单.为了产生一个情节,你需要两行

On the other hand, matplotlib is really easy. In order to produce a plot you need two lines

import matplotlib.pyplot as plt
plt.plot([1,2,3],[2,1,3])

完全按照需要设置大部分参数.您越想自定义此图,您必须应用的设置就越多.这很好,因为它允许用户自己决定他想要控制情节外观的深度.

which sets most of the parameters exactly as they are needed. The more you want to customize this plot, the more settings you have to apply. Which is fine as it allows the user himself to determine how much in depth he wants to control the appearance of the plot.

大多数 matplotlib 代码可以分为三个部分.

Most matplotlib codes can be separated into three parts.

  1. 设置样式
  2. 创建情节
  3. 自定义情节

在问题代码的情况下设置样式涉及例如刻度标签大小和网格的使用.这些属性可以像在代码中那样设置,但确实可能人们总是想在这里使用相同的属性,并且发现每次创建绘图时都输入相同的参数很烦人.因此 matplotlib 提供了通用的样式设置,称为 rcParams.它们可以在脚本的开头设置,例如

Setting the style in the case of the code from the question involves e.g. the ticklabel size and the use of a grid. Those properties can set as it's done in the code but it may indeed be that one always wants to use the same properities here and finds it annoying to type the same parameters in every time one creates a plot. Therefore matplotlib provides general style settings, called rcParams. They can be set at the beginning of a script, e.g.

plt.rcParams['lines.linewidth'] = 2
plt.rcParams['axes.grid '] = True
plt.rcParams['axes.labelsize'] = 18

并将应用于脚本中的所有绘图.也可以使用这些参数定义完整的样式表.有关详细信息,请参阅自定义 matplotlib 文章.
对于某些应用程序,同样可以使用预定义的样式表.
简单的导入import seaborn也是一种可能的方式来改变样式.

and will be applied to all plots within the script. It is also possible to define a complete stylesheet using those parameters. For more information see the Customizing matplotlib article.
It is equally possible to use predefined stylesheets for certain applications.
Simply importing import seaborn is also a possible way to change the style.

创建情节再简单不过了.很明显,一个人需要与要绘制的项目一样多的绘图命令.创建图形和轴,如

Creating the plot can not be simplified much more. It's clear that one needs as many plotting commands as items to plot. Creating the figure and axes like

fig, ax = plt.subplots()

虽然节省了一行.

如果需要自定义刻度或刻度线,则同样不可能进行简化.然而,可以考虑使用代码和格式器来达到这一目的.

Equally no simplification is possible if customizing ticks or tickmarks are required. One may however consider to use Tickers and Formatters for this purpose.

最后当然可以考虑编写一个自定义函数来执行大部分任务,但每个人都可以决定这是否对自己有用.

At the end one may of course consider to write a custom function which performs much of those tasks, but everyone can decide if that is useful for himself.

这篇关于如何在 Python 上制作简单高效的绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆