Skip to main content
 首页 » 编程设计

python之Matplotlib 在同一轴上超过 3 条线

2025年05月04日14mfrbuaa

如何让 matplotlib 在同一张图上显示 3 个以上的图? 例如我有:

plt.figure(1) 
th=np.linspace(0,pi,num=200) 
y=range(10) 
for i in range(10): 
    alph=np.exp(i/2) 
    y[i]=((np.cos(alph*np.cos(th)) - np.cos(alph))/np.sin(th))**2 
    figure(1) 
    plt.plot(th/pi,y[i]) 
plt.show() 

但我无法让图形一次显示超过 3 行。

请您参考如下方法:

这应该可以,否则请检查您的 matplotlib 版本(这适用于 1.4.3)

import matplotlib.pyplot as plt 
import numpy as np 
from math import * 
 
plt.figure(1) 
th=np.linspace(0,pi,num=200) 
y=range(10) 
plots_y = [] 
for i in range(10): 
    alph=np.exp(i/2) 
    y[i]=((np.cos(alph*np.cos(th)) - 1. * np.cos(alph))/np.sin(th))**2 
    plots_y.append(y[i]) 
 
for plot in plots_y: 
    plt.plot(1. * th/pi, plot) 
plt.show()