python - 使用 Matplotlib 绘制数据子集

标签 python pandas numpy matplotlib plot

我正在使用 matplotlib 在我的 DataFrame 中绘制数据条形图。我使用这个结构首先绘制整个数据集:

import pandas as pd
from collections import Counter
import matplotlib.pyplot as plt 

Temp_Counts = Counter(weatherDFConcat['TEMPBIN_CONS'])
df = pd.DataFrame.from_dict(Temp_Counts, orient = 'index').sort_index()
df.plot(kind = 'bar', title = '1969-2015 National Temp Bins', legend = False, color = ['r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b','r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g' ] )

现在我想绘制相同的数据列,但我想对特定的数据子集进行绘制。对于“region_name”中的每个区域,我想生成条形图。这是我的 DataFrame 的示例。

enter image description here

我尝试的解决方案是写:

if weatherDFConcat['REGION_NAME'].any() == 'South':
    Temp_Counts = Counter(weatherDFConcat['TEMPBIN_CONS'])
    df = pd.DataFrame.from_dict(Temp_Counts, orient = 'index').sort_index()
    df.plot(kind = 'bar', title = '1969-2015 National Temp Bins', legend = False, color = ['r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g', 'b', 'b','r', 'r', 'g', 'g', 'b', 'b', 'r', 'r', 'g', 'g' ] )
    plt.show()

奇怪的是,当我运行这段代码时,它只适用于“南部”地区。对于“South”,生成了情节,但对于任何其他地区,我尝试运行代码(我没有收到错误消息),但情节从未出现。在除南方以外的任何区域运行我的代码都会在控制台中产生此结果。

enter image description here

南部地区是我的 DataFrame 中的第一部分,它有 4000 万行长,其他地区在更下方。我尝试绘制的 DataFrame 的大小是否与此有关?

最佳答案

如果我对你的问题的理解正确,那么你正在尝试在绘图之前做两件事:

  1. 根据 REGION_NAME 过滤。

  2. 在过滤后的数据框中,计算 TEMPBIN_CONS 列中每个值出现的次数。

你可以在 pandas 中做这两件事:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'STATE_NAME': ['Alabama', 'Florida', 'Maine', 'Delaware', 'New Jersey'],
                        'GEOID': [1, 2, 3, 4, 5],
                 'TEMPBIN_CONS': ['-3 to 0', '-3 to 0', '0 to 3', '-3 to 0', '0 to 3'],
                  'REGION_NAME': ['South', 'South', 'Northeast', 'Northeast', 'Northeast']},
                         columns=['STATE_NAME', 'GEOID', 'TEMPBIN_CONS', 'REGION_NAME'])

df_northeast = df[df['REGION_NAME'] == 'Northeast']
northeast_count = df_northeast.groupby('TEMPBIN_CONS').size()

print df
print df_northeast
print northeast_count

northeast_count.plot(kind='bar')
plt.show()

输出:

   STATE_NAME  GEOID TEMPBIN_CONS REGION_NAME
0     Alabama      1      -3 to 0       South
1     Florida      2      -3 to 0       South
2       Maine      3       0 to 3   Northeast
3    Delaware      4      -3 to 0   Northeast
4  New Jersey      5       0 to 3   Northeast

   STATE_NAME  GEOID TEMPBIN_CONS REGION_NAME
2       Maine      3       0 to 3   Northeast
3    Delaware      4      -3 to 0   Northeast
4  New Jersey      5       0 to 3   Northeast

TEMPBIN_CONS
-3 to 0    1
0 to 3     2
dtype: int64

enter image description here

关于python - 使用 Matplotlib 绘制数据子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173034/

相关文章:

python - 从字典 python 中选择一个键

python - 获取 Pandas 系列字符串的第一个元素

python - 读取带有合并单元格标题的 Excel 文件时修复 DataFrame 列

python - 计算每个值本身的大于操作的有效方法

python-2.7 - 3 维 numpy 数组到多索引 Pandas 数据框

python - 索引错误: index 0 is out of bounds for axis 0 with size 0

python - 将元素列表转换为 int 而不是 python 中的字符串

python - Pybrain:自定义错误/性能函数?

python - pandas 数据框的包装列名称

python - 如何重复 pandas 数据框列 x 次?