首页 > 解决方案 > iPython shell 不会从 pandas DataFrame 渲染 matplotlib 直方图

问题描述

当我的 py 脚本通过 iPython 或终端运行时,它会停止(不会终止)并且不会从我的数据中呈现直方图。我的df中我特别感兴趣的栏目是状态码栏。我正在尝试生成具有状态 ID 频率的直方图。只有这个非数字列 hist 函数不会从我的数据中呈现。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as scs
from collections import Counter
import seaborn as sb

#data comes from https://www.kaggle.com/usdot/pipeline-accidents/data
oil_df = pd.read_csv('data/pipeline-accidents.csv')

感兴趣的数据列的摘录:出:accident_state 0 KS

1个我

2 洛杉矶

3 威斯康星

4 德克萨斯州

5 检测不到

#any suggestions on doing this more efficiently would be welcomed as well!
cols = df.columns.tolist()
cols = [col.lower() for col in cols]
cols = [col.replace(' ','_') for col in cols]
cols = [col.replace('(','') for col in cols]
cols = [col.replace(')','') for col in cols
oil_df.columns=cols

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)
states = oil_df1['accident_state'].copy()
ax.hist(states,bins=50)
plt.show(fig)

当我手动终止程序时,出现以下错误:

   /anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, 
   *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, 
    func.__name__),
       1854                         RuntimeWarning, stacklevel=2)
    -> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

   /anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in 
hist(***failed resolving arguments***)
   6604                 patch = _barfunc(bins[:-1]+boffset, height, width,
   6605                                  align='center', log=log,
-> 6606                                  color=c, **{bottom_kwarg: bottom})
   6607                 patches.append(patch)
   6608                 if stacked:

/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, 
*args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, 
func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, 
*args, **kwargs)
   2326             ymin = max(ymin * 0.9, 1e-100)
   2327             self.dataLim.intervaly = (ymin, ymax)
-> 2328         self.autoscale_view()
   2329 
   2330         bar_container = BarContainer(patches, errorbar, label=label)

/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in 
autoscale_view(self, tight, scalex, scaley)
   2448             stickies = [artist.sticky_edges for artist in 
self.get_children()]
   2449             x_stickies = sum([sticky.x for sticky in stickies], [])
-> 2450             y_stickies = sum([sticky.y for sticky in stickies], [])
   2451             if self.get_xscale().lower() == 'log':
   2452                 x_stickies = [xs for xs in x_stickies if xs > 0]

谢谢!

标签: pythonpandasmatplotlibipython

解决方案


推荐阅读