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

Zak*_*Zak 3 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
Run Code Online (Sandbox Code Playgroud)

数据是保密的,所以我无法分发.希望有人可以帮我弄清楚该做什么而不需要它.

编辑:有趣的是,matplotlib弹出的"原生"情节查看器显示各个图形的正确图像...每个图形只有1个.但是,保存的图像在每个图形上都有多个图形.

mwa*_*kom 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")
Run Code Online (Sandbox Code Playgroud)

哪个产生:

充分 第1部分 第2部分 第三部分