简体   繁体   English

使用Python Matplotlib将一个大图转换成几个小图

[英]Converting one large figure into several smaller figures using Pythons Matplotlib

I have an 'x' column (x_parameter) and multiple 'y' columns (filtered_data) and I'm trying to produce a scatter plot for each x,y pair. 我有一个“ x”列(x_parameter)和多个“ y”列(filtered_data),我试图为每个x,y对生成散点图。 I have done this successfully in the following function: 我已经在以下功能中成功完成了此操作:

def scatter_plot(self,filtered_data,x_parameter):
    for i in range(len(filtered_data)):
        if filtered_data[i].name==x_parameter:
            x=filtered_data[i]
            x_index=list(x.keys())
    figure()
    for i in range(len(filtered_data)):
        y=filtered_data[i]
        y_index=list(y.keys())
        index_intersection = list(set(x_index)&set(y_index))
        subplot(10,5,i)
        scatter(x[index_intersection],y[index_intersection])

This produces one large figure with 43 sublots. 这将产生一个带有43个子批的大图形。 I am now trying to change this so that I produce 3 smaller figures with two 4x4 subplots and the remainder in the last. 我现在正在尝试更改此设置,以便生成3个较小的图形,其中包含两个4x4子图,其余的在最后一个。 I would also like this function to be dynamic and be able to handle any inputs of any size and still return figure 'units' of 16 subplots and a remainder figure. 我还希望此功能是动态的,并且能够处理任何大小的任何输入,并且仍返回16个子图的图形“单位”和其余图形。 My attempt at doing this is below: 我的尝试如下:

def scatter_plot(self,filtered_data,x_parameter):
    number_of_full_subplots=len(filtered)/16
    remainder=len(filtered)-(number_of_full_subplots*16)
    for i in range(len(filtered_data)):
        if filtered_data[i].name==x_parameter:
            x=filtered_data[i]
            x_index=list(x.keys())
    for j in range(number_of_full_subplots+1):
        figure(j)
        for i in range(len(filtered_data)):
            y=filtered_data[i]
            y_index=list(y.keys())
            index_intersection = list(set(x_index)&set(y_index))
            x_to_plot=x[index_intersection]
            y_to_plot=y[index_intersection]
            for k in range(16):
                plt.subplot(4,4,k)
                plt.scatter(x_to_plot,y_to_plot)

However this produces 3 figures of the appropiate size but with the same graph in each space. 但是,这会生成3个适当大小的图形,但每个空间中的图形相同。 Could anybody spot my mistake? 有人可以发现我的错误吗?

Here is an example of the 'filtered_data' variable. 这是'filtered_data'变量的示例。 It was created by another function and is a list of pandas series. 它是由另一个函数创建的,是熊猫系列的列表。 For each x and y pair I only plot the data if the indices appear in both series. 对于每个x和y对,我仅在索引同时出现在两个系列中时才绘制数据。

Name: RAR activation, dtype: float64, 0     168.806000
2     160.569000
4     175.428000
6      67.584900
7     218.879000
9       2.542630
11      1.822950
12      1.684010
14      0.818888
15      0.032629
21      0.001601
23    192.563000
Name: RAR deactivation, dtype: float64, 6     30.6522
7     30.7873
8     30.8454
9     30.9947
10    31.0030
11    31.1428
12    31.1922
13    31.2839
14    31.3500
15    31.5069
16    31.5113
17    31.5594
Name: Best Value, dtype: float64]

Thanks 谢谢

def scatter_x_y(self,filtered_data,x_parameter,y_parameter):
    for i in range(len(filtered_data)):
        if filtered_data[i].name==x_parameter:
            x=filtered_data[i]
            x_index=list(x.keys())
        elif filtered_data[i].name==y_parameter:
            y=filtered_data[i]
            y_index=list(y.keys())
    index_intersection=list(set(x_index)&set(y_index))
    plt.figure
    plt.scatter(x[index_intersection],y[index_intersection])
    plt.title('{} Versus {}'.format(x.name,y.name))


def scatter_x_yi(self,filtered_data,x_parameter):
    PE=Parameter_Estimation_Tools()
    number_of_full_subplots=len(filtered_data)/16
    remainder=len(filtered)-(number_of_full_subplots*16)-1
    yi=[]
    for i in range(len(filtered_data)):
        if filtered_data[i].name!=x_parameter:
            yi.append(filtered_data[i])      
    try:
        for i in range(len(filtered_data)):
            for j in range(number_of_full_subplots+1):
                plt.figure(j)
                for k in range(16):
                    plt.subplot(4,4,k)
                    PE.scatter_x_y(filtered_data,x_parameter,yi[16*j+k].name)
    except IndexError:
        print '{} has been plotted against all other parameters'.format(x_parameter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM