如何为不同的轴参数多次重绘 pyplot

How to redraw a pyplot multiple times for different axis parameters

我正在使用 IPython 笔记本。我在同一个图中有一堆图。 我需要使用不同的轴参数来显示这些图,即对于以下四种情况:

Cases  x-axis   y-axis
1      non-log  non-log
2      non-log  log
3      log      non-log
4      log      log

有没有这样简单的方法:

#many lines of code for generating bunch of plots
plt.show()

#figure shown with non-log axis

ax.set_yscale('log')
plt.show()

#figure shown with log y-axis    

ax.set_xscale('log')
plt.show()

#figure shown with log x-axis and log y-axis

plt.show() 将显示图形并随后丢弃它们。它不适合在脚本中多次使用。

您可以选择在函数中绘图,并根据参数创建不同的图形。以下将为所有 4 个案例创建数字:

import matplotlib.pyplot as plt
import numpy as np

x = np.logspace(0,3, 250)
y = .4*x

def plot(x,y,logx=False, logy=False):
    fig, ax = plt.subplots()
    if logy: ax.set_yscale('log')
    if logx: ax.set_xscale('log')
    ax.plot(x,y)


plot(x,y)
plot(x,y, True, False)
plot(x,y, False, True)
plot(x,y, True, True)

plt.show()