使用pyplot创建自定义绘图函数



我想定义一个接受输入ax并在其上绘制数据的函数。

我的快速伪示例,在spyder 4(后端:PyQT5(上工作:

from matplotlib import pyplot as plt
def func(L, ax):
L_modified = [x+k for k, x in enumerate(L)]
ax.plot(L_modified)
return L_modified
f, ax = plt.subplots(1, 1)
L = [1, 2, 3, 4, 5]
data = func(L, ax)

这很好,data是一个包含修改后的输入的列表;并且绘图正确地出现。在不同的帖子/解释中,我看到绘制绘图的函数通常返回ax,但我的情况并非如此。是否有必要返回ax以使这种类型的函数适用于每个IDE?它工作只是因为我在用间谍软件吗?

由于我的问题语法对一些人来说似乎有点过于宽泛,这里有一个大的一体式:创建自定义绘图函数的最佳方法是什么

EDIT:如果需要,我创建图形的示例

def butter_lowpass_filter(self, cutoff, order=5, plot=False, ax=None, **plt_kwargs):
nyq = 0.5 * self.fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
data_filtered = lfilter(b, a, self.data)

if plot is True:
if ax is None:
f, ax = plt.subplots(2, 1, sharex=True, sharey=True, figsize=(15, 10))
f.subplots_adjust(wspace=0, hspace=0.15)
ax[0].plot(self.data, color='blue')
ax[0].plot(data_filtered, color='teal')
ax[0].set_title("Signal + Fitlered signal")
ax[1].plot(data_filtered, color='teal')
ax[1].set_title("Filtered signal with a Butterworth filter")

else:
ax.plot(data_filtered, **plt_kwargs)
ax.set_title("Filtered signal with a Butterworth filter")

return data_filtered

我不确定我是否明白你在找什么。我想你想要一个修改图形的函数。代码有两个版本,第二个版本有轻微的注释,第一个版本有改进。

  1. 我不使用spyder(只使用终端(,并且我没有您的代码的图形结果。

  2. 总的来说,我认为如果您使用matplotlib,完全定义图形窗口(图(并且不让python在幕后完成工作是一种很好的做法。如果程序变得复杂,您将需要定义。

  • 图形的名称很重要
  • 图使用多个坐标系时的add_subplot
  • 尝试根据图形对象固定轴的边界
  1. 我从你的开始写了一个最小的例子。它基于您的函数,该函数被多次调用。因此,图像不断演变。现在,你得到的是一些基本的动画。但是,即便如此,你也可以很容易地看到许多缺陷:
  • 轴不稳定
  • 图表堆积如山

功能应该改进;因为我不知道我的方向是否正确,所以我暂时停在这里。

  1. 为什么函数会返回该列表

第一个版本

import matplotlib as mpl
from matplotlib import pyplot as plt
import time
def func(L, fig):
L_modified = [x+k for k, x in enumerate(L)]
ax = fig.axes[0]
ax.plot(L_modified)
return L_modified
plt.ion()
delta_t = .5
f = plt.figure(figsize=(8, 6))
ax = f.add_subplot(1,1,1, aspect='equal')
L = [1, 2, 3, 4, 5]
data = func(L, f)
plt.pause(delta_t)
for j in range(len(L)):
L[j] = L[j]-3*(j+1) 
data = func(L, f)
plt.pause(delta_t)
f.show()
plt.show(block=True)

第二,代码的改进和注释版本。我也在使用函数的输出。

import matplotlib as mpl
from matplotlib import pyplot as plt
import time
# I modified the function such that the graphical changes that it induces
# are all realised when the function is called.
def func(L, fig): 
L_modified = [x+k for k, x in enumerate(L)]
ax = fig.axes[0]
curves = ax.get_lines()  # list of curves in axes
if len(curves)==0:
ax.plot(L_modified)
else:
curves[-1].set_ydata(L_modified)  # modify the y data of the curve
plt.show()
plt.pause(delta_t)  # pause for delta_t seconds
return L_modified
plt.ion()  # turn the interactive mode on.
f = plt.figure(figsize=(8, 6))
ax = f.add_subplot(1,1,1, aspect='equal')
ax.set(xlim=(-1, 5), ylim=(-5.25, 9.25))  # fix the drawing region
delta_t = .5
L = [1, 2, 3, 4, 5]
data = func(L, f)
plt.pause(delta_t)  # pause for delta_t seconds
for j in range(len(L)):
L[j] = -data[j] 
data = func(L, f)

plt.show(block=True)
'''
Display all open figures and change block to True; it was on False due
to plt.ion().
'''

最新更新