Python 从一个脚本生成多个单独的图形

Python 从一个脚本生成多个单独的图形,python,matplotlib,Python,Matplotlib,我正在处理一个大型数据集;完整的数据集需要相当长的时间来获取所有的x和y值,因此我尝试在每次运行时生成多个图。我试图生成完整数据集的两个图形以及每一行的图形 然而,我很难让它工作。我所做的一切都以完整的图形结束,完美地工作,然后是一系列不太独立的“独立”图形-第一个生成的图形只有一行,但第二个有第一行和第二行:图形没有正确地“清除” import pandas as pd import numpy as np import matplotlib.pyplot as plt import re i

我正在处理一个大型数据集;完整的数据集需要相当长的时间来获取所有的x和y值,因此我尝试在每次运行时生成多个图。我试图生成完整数据集的两个图形以及每一行的图形

然而,我很难让它工作。我所做的一切都以完整的图形结束,完美地工作,然后是一系列不太独立的“独立”图形-第一个生成的图形只有一行,但第二个有第一行和第二行:图形没有正确地“清除”

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
import seaborn as sns

groupFig = plt.figure(num=None, figsize=(10,10), dpi=80, facecolor='w', edgecolor='k') # Set up the group figure, for all of the data

df = pd.read_csv('cdk.csv') # Get the data

l = 0 # some counters
m = 0

for i in range(0,len(df.index)):
    rowKeys = df.iloc[i].keys()

    singleFig = plt.figure(num=None, figsize=(10,10), dpi=80, facecolor='w', edgecolor='k') # Set up the single figure, for each individual row of data. I put it in the loop thinking it might recreate it every time, but to no avail.
    ax2 = singleFig.add_subplot(111) # I think I need this to have multiple series on one graph

    x=[] # open array for x and y
    y=[]

    for j in range(0,len(df.iloc[i])): # for all the values in the row
        if rowKeys[j].startswith("Venus_Activity at") and pd.isnull(df.iloc[i][j]) == False: # Scrape rows that contain y data, but only if the data isn't NaN
            y.append(df.iloc[i][j]) # add y values to the array
            x.extend(re.findall('\d+\.?\d*', rowKeys[j])) # scrape only the number from the row, use it as x
            x = map(float,x) # but they have to be float in order to work later

    ax1.plot(x, y) # for each compound, plot into my group figure
    ax2.plot(x, y) # for each compound, plot into the single figure
    groupFig.savefig(r'Plot/cdk/Plot' + str(i) + '.png') # save each single figure individually
    # ax2.cla() # I want to clear the figure here, but it doesn't work. It wants plt.cla() but that effects both figures...

groupFig.savefig(r'Plot/cdk/CDK plot.png') # Save the completed group figure
plt.close() # clean up
数据是保密的,所以我不能分发。希望有人能在不需要的情况下帮我弄清楚该怎么做


编辑:有趣的是,matplotlib弹出的“本机”绘图查看器为各个图形显示正确的图像。。。每个图形只有1个。但是,保存的图像在每个图形上都有多个绘图。

我认为您在使用面向对象的界面时存在一些缺陷,即绘图将转到哪个轴以及保存哪些图。以下是我复制基本想法的尝试:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

full_fig, full_ax = plt.subplots()
x = np.arange(5)

for i, color in zip(range(1, 4), sns.color_palette()):

    part_fig, part_ax = plt.subplots(subplot_kw=dict(ylim=(0, 12)))
    y = x * i
    full_ax.plot(x, y, c=color)
    part_ax.plot(x, y, c=color)
    part_ax.set_title("Part %d" % i)
    part_fig.savefig("part_%d.png" % i)
full_ax.set_title("Full")
full_fig.savefig("full_png")
产生:

这似乎对我有用(这是我找到这篇文章时一直在寻找的)-

只要使用

plt.plot(x\u列表,y\u列表)
plt.show()
然后使用

plt.plot(x_列表2,y_列表2)
plt.show()

你会没事的。

你确定这是你正在使用的逐字脚本吗?我认为在
groupFig.plot(x,y,color=color,…)
上会出现错误,因为matplotlib图形对象没有
plot
属性(至少在matplotlib 1.3上是这样)。您是对的。它们应该是ax1和ax2。我试过东西后忘了把它换回来。这确实管用。不过,我有点难以回到我的图表以前的样子。例如,这不再使用
plt.figure(num=None,figsize=(10,10),dpi=80,facecolor='w',edgecolor='k')
,如何重新实现大小和内容?x和y标签的问题相同。出于某种原因,
plt.xlabel('uM component')
只影响组图,而不影响单个组图。
f,ax=plt.subplot()
只是
f=plt.figure()的缩写;ax=f.add_子批次(111)
。您可以用更详细的方式来完成,也可以查看
子批
docstring以了解如何传递这些参数(应该直接或在
子批\u kw
字典中使用其中的大多数标签。对于标签,您还需要使用面向对象的接口,以便您可以控制它们的去向。例如,不要执行
plt.xlabel()
,而是执行
full\u fig.set\u xlabel()
。太好了!谢谢你的帮助。太好了,准确地回答了问题。
import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(4,3))
plt.plot(list(x for x in range(1,50,5)), list(x*x for x in range(1,11)))

plt.figure(figsize=(8,6))
plt.plot(list(x for x in range(1,100,5)), list(x*x for x in range(1,21)))