我正在尝试创建一个绘图函数,该函数将所需绘图的数量作为输入并使用pylab.subplots
和sharex=True
选项绘制它们.如果所需图的数量是奇数,那么我想删除最后一个面板并强制它上面的面板上的刻度标签.我找不到这样做的方法并同时使用该sharex=True
选项.子图的数量可能非常大(> 20).
这是示例代码.在这个例子中,我想在何时强制xtick标签i=3
.
import numpy as np
import matplotlib.pylab as plt
def main():
n = 5
nx = 100
x = np.arange(nx)
if n % 2 == 0:
f, axs = plt.subplots(n/2, 2, sharex=True)
else:
f, axs = plt.subplots(n/2+1, 2, sharex=True)
for i in range(n):
y = np.random.rand(nx)
if i % 2 == 0:
axs[i/2, 0].plot(x, y, '-', label='plot '+str(i+1))
axs[i/2, 0].legend()
else:
axs[i/2, 1].plot(x, y, '-', label='plot '+str(i+1))
axs[i/2, 1].legend()
if …
Run Code Online (Sandbox Code Playgroud)