【发布时间】:2017-01-03 12:34:41
【问题描述】:
我正在使用 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 的示例。
我尝试的解决方案是这样写:
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 区域是我的 DataFrame 中的第一部分,它有 4000 万行长,其他区域更靠后。我尝试绘制的 DataFrame 的大小与此有关吗?
【问题讨论】:
-
您是否尝试使用具有其他名称的比较表达式将一个区域提取到另一个数据帧?这行得通吗?
标签: python pandas numpy matplotlib plot