python第二大奇数_python-2.7 – matplotlib,包含奇数个子图
我正在嘗試創建一個繪圖函數,該函數將所需繪圖的數量作為輸入,并使用pylab.subplots和sharex = True選項繪制它們.如果所需圖的數量是奇數,那么我想刪除最后一個面板并強制它上面的面板上的刻度標簽.我找不到這樣做的方法,同時使用sharex = True選項.子圖的數量可以非常大(> 20).
這是示例代碼.在這個例子中,我想在i = 3時強制xtick標簽.
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 n % 2 != 0:
f.delaxes(axs[i/2, 1])
f.show()
if __name__ == "__main__":
main()
總結
以上是生活随笔為你收集整理的python第二大奇数_python-2.7 – matplotlib,包含奇数个子图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: string转换为bigdecimal_
- 下一篇: pythonc语言结构_C语言结构体笔记