我是 Python 新手,過去主要使用 MatLab。我正在重新撰寫我的一個 MatLab 腳本,并且想知道如何將繪圖添加到圖形中。似乎在 python 中,我一次只能打開一個圖形,并且必須在打開第二個圖形之前手動關閉視窗。我的原始腳本有幾百行長,但這是我想做的 MWE。
import matplotlib.pyplot as plt
import numpy as np
#from mpl_toolkits import mplot3d
lst = [ 1, 1.5, 2, 4.5]
alpha= np.array(lst)
#initialize tables for plots
xtable = []
ytable = []
y2table = []
#determine whether lst is a vector or an array for number of iterations of inner and outer loops
def size(arr):
if len(arr.shape) == 1:
return arr.shape[0], 1
return arr.shape
[nn,mm] = size(alpha)
#create and plot data
for kk in range(nn):#= 1:nn
x = [i for i in range(0, 10)]
y = [alpha[kk]*i for i in range(0, 10)]
y2 = [alpha[kk]*i**2 for i in range(0, 10)]
#data for plot(s)
xtable = [x]
ytable = [y]
y2table = [y2]
#plot1
plt.plot(xtable,ytable)
plt.hold on
#plot2
plt.plot(xtable,y2table)
plt.hold on
在我的腳本中,這些實際上是 3D 圖,但我認為這里沒有必要。我只希望 for 回圈為 lst 中的每個值運行并最終得到兩個數字,每個數字都有 4 個圖。lst 的大小不是固定的,否則我會在回圈中生成資料并稍后繪制。
預先感謝您的幫助
uj5u.com熱心網友回復:
跟進 tdy 的評論:
#create plots:
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
#plot data
for kk in range(nn):#= 1:nn
x = [i for i in range(0, 10)]
y = [alpha[kk]*i for i in range(0, 10)]
y2 = [alpha[kk]*i**2 for i in range(0, 10)]
#data for plot(s)
xtable = [x]
ytable = [y]
y2table = [y2]
#plot1
ax1.plot(xtable,ytable)
#plot2
ax2.plot(xtable,y2table)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/446583.html
標籤:Python python-3.x matlab matplotlib 数字
上一篇:在函式內更改向量中的一個元素
