Python如何在循环中创建多个绘图?

Python如何在循环中创建多个绘图?,python,matplotlib,networkx,Python,Matplotlib,Networkx,Python创建多个绘图的方法是什么 我已经在下面编写了一些带有绘图变量的代码。我的直觉是编写一个对象数组来迭代。Python中的对象似乎比我已经习惯的Javascript/JSON对象要复杂一些 有什么关于“Python方式”的提示来完成我在这里需要做的事情吗 import pandas as pd
 import networkx as nx from networkx.algorithms import community import matplotlib.p

Python创建多个绘图的方法是什么

我已经在下面编写了一些带有绘图变量的代码。我的直觉是编写一个对象数组来迭代。Python中的对象似乎比我已经习惯的Javascript/JSON对象要复杂一些

有什么关于“Python方式”的提示来完成我在这里需要做的事情吗

import pandas as pd
            
import networkx as nx 
from networkx.algorithms 
import community
import matplotlib.pyplot as plt
from datetime import datetime
         

graph = pd.read_csv('C:/Users/MYDIR/MYSOURCE.csv')
filename_prefix = datetime.now().strftime("%Y%m%d-%H%M%S")

#begin stuff that changes every plot
filename_suffix = 'suffix_one'
directory = 'C:/Users/MYDIR/';
    
title='MY_TITLE'
    
axis1='AXIS1';
    
axis2='AXIS2';
    
color = 'lightgreen';
#end stuff that changes every plot

df = graph[[axis1,axis2]]
G = nx.from_pandas_edgelist(df, axis1, axis2 );
plt.figure(figsize=(10,5))
    
ax = plt.gca()
ax.set_title(title)
nx.draw(G,with_labels=True, node_color=color, ax=ax)
_ = ax.axis('off')

#plt.show()
    
plt.savefig(directory + filename_prefix +'_' + title);
#plt.savefig(filename_prefix +'_' + filename_suffix + '.png')

数组是python中的列表,它们是可编辑的。如果计划使用这些对象的列表,则可以遍历每个元素并保存结果图?您希望对每个绘图都做一些特殊的处理吗?

我修改了中的代码,创建了一些接近我认为可行的东西:

import numpy as np
    
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 10)
y = x * x

#fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True)

titles = ['Linear', 'Squared', 'Cubic', 'Quartic']
y_vals = [x, x * x, x**3, x**4]

# axes.flat returns the set of axes as a flat (1D) array instead
    
# of the two-dimensional version we used earlier
    

for title, y in zip(titles, y_vals):
        
    fig, ax = plt.subplots()
        
    ax.plot(x, y)
        
    ax.set_title(title)
        
    ax.grid(True)
        
    plt.savefig('C:/MyData/' +title + '_.png')
    

多个子批次到底是什么?在本例中,您似乎只有一个图形。虽然这里有一个一般情况的示例,但我希望对每个数据集执行完全相同的操作。在其他语言中,我会把绘图参数放在一个数组中,把绘图创建代码放在一个循环中!