简体   繁体   English

Matplotlib子图功能:使用for循环在子图的矩阵中绘制每一行

[英]Matplotlib subplots function: plot each row in a matrix in a subplot using for loop

I have a function that returns a matrix with varying rows but constant columns (10) eg (5, 10), (7, 10) , (20,10) etc. 我有一个函数,该函数返回具有变化的行但具有恒定列(10)的矩阵,例如(5,10),(7,10),(20,10)等。

Plotting the matrix using a constant x-axis matrix yields the following plot 1 使用恒定的x轴矩阵绘制矩阵可得出以下曲线1

Now, trying to use code to add each plot into a separate subplot 现在,尝试使用代码将每个图添加到单独的子图中

#the constant x-axis is
myx = [  6.89668072, 6.79190465,  6.48075998e+00 ,  5.97270071, 5.28316394 , 4.43310092,   3.44834036,  2.35880373,  1.19759604e+00 ,  4.22299899e-16]
#the y axis for this example is 
finalyy =[np.array([ 0.        ,  0.19801812,  0.32703622,  0.39731833,  0.43205176,
0.44652588,  0.44920819,  0.44348252,  0.430474  ,  0.40601885]), np.array([ 0.        ,  0.18017484,  0.30180713,  0.37321907,  0.41381173,
0.43625179,  0.44750785,  0.44986628,  0.44364735,  0.42256948]), np.array([ 0.        ,  0.16233156,  0.27657803,  0.3491198 ,  0.3955717 ,
0.4259777 ,  0.44580751,  0.45625005,  0.4568207 ,  0.43912011]), np.array([ 0.        ,  0.14448829,  0.25134894,  0.32502053,  0.37733167,
0.41570361,  0.44410717,  0.46263381,  0.46999405,  0.45567074]), np.array([ 0.        ,  0.12664501,  0.22611984,  0.30092126,  0.35909164,
0.40542952,  0.44240682,  0.46901757,  0.4831674 ,  0.47222137])]

#getting the value of rows in finalyy matrix
last = np.array(finalyy).shape
finalval = last[0]
m = 3 #used below to define number of subplots

#creating subplot numbers eg 331 332 333 334 335 etc
empty = []
for x in range (1,finalval+1):
    mat = (str(m)+ str(m)+str(x))
    empty.append(mat)
finalempty = np.asarray(empty)

#trying to plot each row in finalyy matrix whilst using each of the subplot index above
fig = plt.figure()
for row in finalyy:
    for j in finalempty:
      ax1 = fig.add_subplot(j)

    ax1.plot(myx,row,  'r-')

plt.tight_layout()
plt.show()

the code also generates this Plot 2 代码也会生成该图2

I want to plot each curve in a subplot using a function. 我想使用函数在子图中绘制每条曲线。

ax1.plot(myx,row, 'r-') plots all rows to ax1 , which is the last of the axes. ax1.plot(myx,row, 'r-')所有行绘制到ax1 ,这是轴的最后一个。

Your problem can be simplified by using plt.subplots to create the subplots. 使用plt.subplots创建子图可以简化您的问题。

import numpy as np
import matplotlib.pyplot as plt
myx = [  6.89668072, 6.79190465,  6.48075998e+00 ,  5.97270071, 5.28316394 , 4.43310092,  
         3.44834036,  2.35880373,  1.19759604e+00 ,  4.22299899e-16]
#the y axis for this example is 
finalyy =[np.array([ 0.        ,  0.19801812,  0.32703622,  0.39731833,  0.43205176,
                    0.44652588,  0.44920819,  0.44348252,  0.430474  ,  0.40601885]), 
            np.array([ 0.        ,  0.18017484,  0.30180713,  0.37321907,  0.41381173,
                      0.43625179,  0.44750785,  0.44986628,  0.44364735,  0.42256948]), 
            np.array([ 0.        ,  0.16233156,  0.27657803,  0.3491198 ,  0.3955717 ,
                      0.4259777 ,  0.44580751,  0.45625005,  0.4568207 ,  0.43912011]), 
            np.array([ 0.        ,  0.14448829,  0.25134894,  0.32502053,  0.37733167,
                      0.41570361,  0.44410717,  0.46263381,  0.46999405,  0.45567074]),
            np.array([ 0.        ,  0.12664501,  0.22611984,  0.30092126,  0.35909164,
                      0.40542952,  0.44240682,  0.46901757,  0.4831674 ,  0.47222137])]

m = 3 #used below to define number of subplots

fig, axes = plt.subplots(nrows=m, ncols=m)
for ax, row in zip(axes.flatten(), finalyy):
    ax.plot(myx,row,  'r-')

# turn remaining axes off
for i in range(len(finalyy),m**2):
    axes.flatten()[i].axis("off")

plt.tight_layout()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 matplotlib 遍历循环并为每次迭代绘制子图 - Iterating over a loop and plot subplot for each iterations using matplotlib 如何使用 matplotlib 子图为每一行绘制折线图? - How do I plot a line graph for each row using matplotlib subplots? 子图子图Matplotlib / Seaborn - Subplot of Subplots Matplotlib / Seaborn Matplotlib - 向子图中添加子图? - Matplotlib - adding subplots to a subplot? MatPlotLib - 子图的子图或单个图上的多个断轴图 - MatPlotLib - Subplots of subplot or multiple broken axis charts on single plot matplotlib 使用子图(如网格、行或列)绘制多个图 - matplotlib plot multiple plots using subplots like grid, in row or in column 如何通过使用自定义绘图功能对Matplotlib表进行子绘图? - How to subplot a matplotlib table by using a custom plot function? Plot 在使用 for 循环的一系列子图中每个 plot 的多个数据 - Plot multiple data on each plot in series of subplots using for loop matplotlib中的plot_date和多列子图。 如何使用plot_date将1列更改为2列子图? - plot_date and multiple column subplots within matplotlib. How to change 1 column to 2 column subplot using plot_date? 多个matplotlib子图:将熊猫数据框HTML与每个子图交错 - multiple matplotlib subplots: interleave pandas dataframe HTML with each subplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM