循环中的散点图仅绘制第一次迭代

时间:2017-05-29 04:23:45

标签: python matplotlib scatter

我试图通过绘制几个子图并迭代其余类别来绘制散点图中的几个不同的东西,但是这些图只显示第一次迭代而不会抛出任何错误。为了澄清,这里有一个数据实际情况的例子:

          a kind state property   T
0  0.905618    I   dry    prop1  10
1  0.050311    I   wet    prop1  20
2  0.933696   II   dry    prop1  30
3  0.114824  III   wet    prop1  40
4  0.942719   IV   dry    prop1  50
5  0.276627   II   wet    prop2  10
6  0.612303  III   dry    prop2  20
7  0.803451   IV   wet    prop2  30
8  0.257816   II   dry    prop2  40
9  0.122468   IV   wet    prop2  50

这就是我生成示例的方式:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

kinds = ['I','II','III','IV']
states = ['dry','wet']
props = ['prop1','prop2']
T = [10,20,30,40,50]
a = np.random.rand(10)
k = ['I','I','II','III','IV','II','III','IV','II','IV']
s = ['dry','wet','dry','wet','dry','wet','dry','wet','dry','wet']
p = ['prop1','prop1','prop1','prop1','prop1','prop2','prop2','prop2','prop2','prop2']
t = [10,20,30,40,50,10,20,30,40,50]
df = pd.DataFrame(index=range(10),columns=['a','kind','state','property','T'])
df['a']=a
df['kind']=k
df['state']=s
df['property']=p
df['T']=t
print df

接下来,我将生成2行和2列子图,以显示在干湿状态下property1和property2的可变性。所以我基本上将我的数据帧分成几个较小的数据帧:

first = df[(df['state']=='dry')&(df['property']=='prop1')]
second = df[(df['state']=='wet')&(df['property']=='prop1')]
third = df[(df['state']=='dry')&(df['property']=='prop2')]
fourth = df[(df['state']=='wet')&(df['property']=='prop2')]
dfs = [first,second,third,fourth]

在每个子图中,指定了某些实验室条件,我想绘制aT的值,用于不同类型的样本。为了区分样品种类,我为它们分配了不同的颜色和标记。所以这是我的绘图剧本:

fig = plt.figure(figsize=(8,8.5))
gs = gridspec.GridSpec(2,2, hspace=0.4, wspace=0.3)
colours = ['r','b','g','gold'] 
symbols = ['v','v','^','^']
titles=['dry 1','wet 1','dry 2','wet 2']
for no, df in enumerate(dfs):
    ax = fig.add_subplot(gs[no])
    for i, r in enumerate(kinds):
        #print i, r
        df = df[df['kind']==r]
        c = colours[i]
        m = symbols[i]
        plt.scatter(df['T'],df['a'],c=c,s=50.0, marker=m, edgecolor='k')

    ax = plt.xlabel('T')
    ax = plt.xticks(T)
    ax = plt.ylabel('A')
    ax = plt.title(titles[no],fontsize=12,alpha=0.75)
plt.show()

但结果只绘制了第一次迭代,在本例中为红色三角形中的I种。如果我从迭代列表中删除第一个项目,它只会绘制第一个变量(蓝色三角形中的种类II)。

我做错了什么? 这个图看起来像这样,但我希望每个子图都相应地填充红色和蓝色以及绿色和金色标记。 Just the red

(请注意,这也与我的实际数据有关,因此问题不应该与我生成示例的方式有关。)

1 个答案:

答案 0 :(得分:4)

您的问题在于内部for循环。通过撰写df = df[df['kind']==r],您可以将原始df替换为I过滤的版本。然后,在循环的下一次迭代中,您将过滤II,无法找到更多数据。因此,您也不会收到任何错误消息,因为代码“正确”。如果你像这样重写相关的代码:

for no, df in enumerate(dfs):
    ax = fig.add_subplot(gs[no])
    for i, r in enumerate(kinds):
        #print i, r
        df2 = df[df['kind']==r]
        c = colours[i]
        m = symbols[i]
        plt.scatter(df2['T'],df2['a'],c=c,s=50.0, marker=m, edgecolor='k')

    ax = plt.xlabel('T')
    ax = plt.xticks(T)
    ax = plt.ylabel('A')
    ax = plt.title(titles[no],fontsize=12,alpha=0.75)

它应该工作得很好。在Python 3.5上进行了测试。