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

时间:2014-02-25 19:20:06

标签: python matplotlib

我正在处理大型数据集;完整数据集需要花费大量时间来搜索所有x和y值,因此我尝试在每次运行时生成多个图形。我正在尝试生成完整数据集的图形以及每个行的图形。

然而,我无法让它发挥作用。我所做的一切都以完整的图形结束,完美地工作,然后是一系列不是那么单独的“个体”图形 - 第一个生成的只有1行,但第二行有第1行和第2行:这个数字没有正确“清算”。

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个。但是,保存的图像在每个图表上都有多个图表。

3 个答案:

答案 0 :(得分:5)

我认为你在使用面向对象的界面时会遇到一些错误,无论你的图表在哪个轴上,你要保存哪些无花果。这是我尝试复制基本想法:

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")

产生:

full part1 part2 part3

答案 1 :(得分:0)

  

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

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)))

Fig-1

enter image description here

答案 2 :(得分:0)

只需使用 plt.plot(x_list,y_list) plt.show() 然后, plt.plot(x_list2,y_list2) plt.show()

你会没事的