Python 如何在循环的每次迭代中将新图形保存为png
我不知道如何使用NetworkX为循环的每次迭代保存一个新的图形png。我借用了这个问题的代码:并对它进行了一些操作。代码如下:Python 如何在循环的每次迭代中将新图形保存为png,python,matplotlib,networkx,Python,Matplotlib,Networkx,我不知道如何使用NetworkX为循环的每次迭代保存一个新的图形png。我借用了这个问题的代码:并对它进行了一些操作。代码如下: import networkx as nx import matplotlib.pyplot as plt fig = plt.figure(figsize=(12,12)) ax = plt.subplot(111) ax.set_title('Graph - Shapes', fontsize=10) G = nx.DiGraph() G.add_node('
import networkx as nx
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,12))
ax = plt.subplot(111)
ax.set_title('Graph - Shapes', fontsize=10)
G = nx.DiGraph()
G.add_node('shape1', level=1)
G.add_node('shape2', level=2)
G.add_node('shape3', level=2)
G.add_node('shape4', level=3)
G.add_edge('shape1', 'shape2')
G.add_edge('shape1', 'shape3')
G.add_edge('shape3', 'shape4')
pos = nx.spring_layout(G)
n = 0
colorses = ['yellow', 'red', 'blue', 'green']
while n < len(colorses):
nx.draw(G, pos, node_size=1500, node_color=colorses[n], font_size=8, font_weight='bold')
plt.tight_layout()
# plt.show()
plt.savefig("Graph.png", format="PNG")
n += 1
将networkx导入为nx
将matplotlib.pyplot作为plt导入
图=plt.图(图尺寸=(12,12))
ax=plt.子批次(111)
ax.set_title('Graph-Shapes',fontsize=10)
G=nx.DiGraph()
G.add_节点('shape1',级别=1)
G.add_节点('shape2',级别=2)
G.add_节点('shape3',级别=2)
G.add_节点('shape4',级别=3)
G.添加边(“形状1”、“形状2”)
G.添加边(“形状1”、“形状3”)
G.添加边(“形状3”、“形状4”)
位置=nx.弹簧布置图(G)
n=0
颜色=[“黄色”、“红色”、“蓝色”、“绿色”]
当n
理想情况下,我希望有四个图像,每个图像具有不同的颜色节点。如果你需要更多的信息,请告诉我。谢谢 第一个建议,通过索引值访问颜色不是“Pythonic”。相反,使用
for
循环:
for color in colors:
print(color)
在循环的每次迭代中,您的代码都会覆盖Graph.png
。要为每次迭代保存新文件,只需在每次迭代中重命名输出文件。一种方法是使用和函数:
import networkx as nx
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,12))
ax = plt.subplot(111)
ax.set_title('Graph - Shapes', fontsize=10)
G = nx.DiGraph()
G.add_node('shape1', level=1)
G.add_node('shape2', level=2)
G.add_node('shape3', level=2)
G.add_node('shape4', level=3)
G.add_edge('shape1', 'shape2')
G.add_edge('shape1', 'shape3')
G.add_edge('shape3', 'shape4')
pos = nx.spring_layout(G)
colors = ['yellow', 'red', 'blue', 'green']
for i, color in enumerate(colors):
nx.draw(G, pos, node_size=1500, node_color=color, font_size=8, font_weight='bold')
plt.tight_layout()
plt.savefig('Graph_{}.png'.format(i), format="PNG")
第一个建议是,通过索引值访问颜色不是“Pythonic”。相反,使用
for
循环:
for color in colors:
print(color)
在循环的每次迭代中,您的代码都会覆盖Graph.png
。要为每次迭代保存新文件,只需在每次迭代中重命名输出文件。一种方法是使用和函数:
import networkx as nx
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,12))
ax = plt.subplot(111)
ax.set_title('Graph - Shapes', fontsize=10)
G = nx.DiGraph()
G.add_node('shape1', level=1)
G.add_node('shape2', level=2)
G.add_node('shape3', level=2)
G.add_node('shape4', level=3)
G.add_edge('shape1', 'shape2')
G.add_edge('shape1', 'shape3')
G.add_edge('shape3', 'shape4')
pos = nx.spring_layout(G)
colors = ['yellow', 'red', 'blue', 'green']
for i, color in enumerate(colors):
nx.draw(G, pos, node_size=1500, node_color=color, font_size=8, font_weight='bold')
plt.tight_layout()
plt.savefig('Graph_{}.png'.format(i), format="PNG")
只需更改输出文件的名称
while n < len(colorses):
nx.draw(G, pos, node_size=1500, node_color=colorses[n], font_size=8, font_weight='bold')
plt.tight_layout()
# plt.show()
plt.savefig("Graph" + str(n) +".png", format="PNG")
n += 1
这不是很好,因为字符串将有空格,但您可以事先调整它只需更改输出文件的名称
while n < len(colorses):
nx.draw(G, pos, node_size=1500, node_color=colorses[n], font_size=8, font_weight='bold')
plt.tight_layout()
# plt.show()
plt.savefig("Graph" + str(n) +".png", format="PNG")
n += 1
这不是很好,因为字符串将有空格,但是你可以事先调整它。顺便说一句,我刚刚使用了这个方法,我添加了一个步骤,使我的标签来自我的文件名。我希望它能帮助别人
files = glob.glob(folder+'\\'+'*.csv', recursive=False)
for file in files:
df1=pd.read_csv(file,header=1,sep=',')
nslice = (sum(1 for line in open(file))-1)/3
base = print(os.path.splitext(os.path.basename(file))[0])
fig = plt.figure()
ax = plt.subplot(111)
c = ax.plot(df1.iloc[0:int(nslice)-1,[22]],df1.iloc[0:int(nslice)-1,[4]],label='Cuticle')
t = ax.plot(df1.iloc[int(nslice):(int(nslice)-1)*2,[22]],df1.iloc[int(nslice):(int(nslice)-1)*2,[4]],label='Tissue')
p = ax.plot(df1.iloc[int(nslice)*2:(int(nslice)-1)*3,[22]],df1.iloc[int(nslice)*2:(int(nslice)-1)*3,[4]],label='Phenanthrene')
title_obj = plt.title(base)
plt.xlabel("Slice #")
plt.ylabel("Avg MGV")
ax.legend()
plt.savefig('plot'+str(os.path.splitext(os.path.basename(file))[0])+'.png')
plt.show()
plt.close('all')
Peace顺便说一句,我刚刚使用了这个方法,并进一步添加了一个步骤,使我的标签来自我的文件名。我希望它能帮助别人
files = glob.glob(folder+'\\'+'*.csv', recursive=False)
for file in files:
df1=pd.read_csv(file,header=1,sep=',')
nslice = (sum(1 for line in open(file))-1)/3
base = print(os.path.splitext(os.path.basename(file))[0])
fig = plt.figure()
ax = plt.subplot(111)
c = ax.plot(df1.iloc[0:int(nslice)-1,[22]],df1.iloc[0:int(nslice)-1,[4]],label='Cuticle')
t = ax.plot(df1.iloc[int(nslice):(int(nslice)-1)*2,[22]],df1.iloc[int(nslice):(int(nslice)-1)*2,[4]],label='Tissue')
p = ax.plot(df1.iloc[int(nslice)*2:(int(nslice)-1)*3,[22]],df1.iloc[int(nslice)*2:(int(nslice)-1)*3,[4]],label='Phenanthrene')
title_obj = plt.title(base)
plt.xlabel("Slice #")
plt.ylabel("Avg MGV")
ax.legend()
plt.savefig('plot'+str(os.path.splitext(os.path.basename(file))[0])+'.png')
plt.show()
plt.close('all')
和平嗯。。。如果您在每次迭代中更改文件名,使
“Graph.png”
依赖于n
,比如plt.savefig(“Graph{}.png.”format(n),format=“png”)
?嗯。。。如果您每次迭代都更改文件名,使“Graph.png”
依赖于n
,比如plt.savefig(“Graph{}.png.”format(n),format=“png”)
?