在一个图python中组合多个图

时间:2017-01-05 19:01:06

标签: python matplotlib plot figure

我的函数返回28个图(图)但我需要将它们分组在一个图上这是我生成28个图的代码

for cat in df.ASS_ASSIGNMENT.unique() :
    a = df.loc[df['ASS_ASSIGNMENT'] == cat]
    dates = a['DATE']
    prediction = a['CSPL_RECEIVED_CALLS']
    plt.plot(dates,prediction)  
    plt.ylabel("nmb_app")
    plt.legend([cat.decode('utf-8')],loc='best')
    plt.xlabel(cat.decode('utf-8'))

1 个答案:

答案 0 :(得分:9)

使用plt.subplots。例如,

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=7, nrows=4)

for i, ax in enumerate(axes.flatten()):
    x = np.random.randint(-5, 5, 20)
    y = np.random.randint(-5, 5, 20)
    ax.scatter(x, y)
    ax.set_title('Axis {}'.format(i))

plt.tight_layout()

稍微深入一点,正如Mauve指出的那样,这取决于你是想在单个图中的单个图中使用28条曲线,还是在一个图中每个都有自己的轴的28个单独的图。

假设您有一个包含28列的数据框df,您可以使用plt.subplots将所有28条曲线放在单个图中,如此,

fig1, ax1 = plt.subplots()
df.plot(color=colors, ax=ax1)
plt.legend(ncol=4, loc='best')

enter image description here

如果您想在一个图中全部使用28个单独的轴,则可以使用plt.subplots这种方式

fig2, axes = plt.subplots(nrows=4, ncols=7)
for i, ax in enumerate(axes.flatten()):
    df[df.columns[i]].plot(color=colors[i], ax=ax)
    ax.set_title(df.columns[i])

enter image description here

此处df看起来像

In [114]: df.shape
Out[114]: (15, 28)

In [115]: df.head()
Out[115]: 
         IYU        ZMK        DRO       UIC       DOF       ASG       DLU  \
0   0.970467   1.026171  -0.141261  1.719777  2.344803  2.956578  2.433358   
1   7.982833   7.667973   7.907016  7.897172  6.659990  5.623201  6.818639   
2   4.608682   4.494827   6.078604  5.634331  4.553364  5.418964  6.079736   
3   1.299400   3.235654   3.317892  2.689927  2.575684  4.844506  4.368858   
4  10.690242  10.375313  10.062212  9.150162  9.620630  9.164129  8.661847   

         BO1       JFN       S9Q    ...          X4K       ZQG       2TS  \
0   2.798409  2.425745  3.563515    ...     7.623710  7.678988  7.044471   
1   8.391905  7.242406  8.960973    ...     5.389336  5.083990  5.857414   
2   7.631030  7.822071  5.657916    ...     2.884925  2.570883  2.550461   
3   6.061272  4.224779  5.709211    ...     4.961713  5.803743  6.008319   
4  10.240355  9.792029  8.438934    ...     6.451223  5.072552  6.894701   

        RS0       P6T       FOU       LN9       CFG       C9D       ZG2  
0  9.380106  9.654287  8.065816  7.029103  7.701655  6.811254  7.315282  
1  3.931037  3.206575  3.728755  2.972959  4.436053  4.906322  4.796217  
2  3.784638  2.445668  1.423225  1.506143  0.786983 -0.666565  1.120315  
3  5.749563  7.084335  7.992780  6.998563  7.253861  8.845475  9.592453  
4  4.581062  5.807435  5.544668  5.249163  6.555792  8.299669  8.036408  

创建
import pandas as pd
import numpy as np
import string
import random

m = 28
n = 15

def random_data(m, n):
    return np.cumsum(np.random.randn(m*n)).reshape(m, n)

def id_generator(number, size=6, chars=string.ascii_uppercase + string.digits):
    sequence = []
    for n in range(number):
        sequence.append(''.join(random.choice(chars) for _ in range(size)))
    return sequence

df = pd.DataFrame(random_data(n, m), columns=id_generator(number=m, size=3))

颜色定义为

import seaborn as sns
colors = sns.cubehelix_palette(28, rot=-0.4)