python - 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]
谢谢!
解决方案
推荐阅读
- python - 布尔索引后如何删除列中的某些值?
- python - paho mqtt 客户端何时丢弃缓冲消息?
- typescript - 打字稿:定义类型,如 keyof,但具有特定的值类型
- javascript - 如何选择用户可以使用 JavaScript 切换到的所有 html 元素?
- cpu - 不懂计算程序任务执行时间的公式
- c# - 有没有办法检测用户是从团队移动应用程序还是桌面应用程序打字?
- css - 为 Newsdaily 的移动版复制 CSS
- android - 颤动通知中的阴影
- node.js - 在嵌入 Electron / Capacitor 跨平台环境的 Angular v9 项目中使用 Node 的 fs 模块
- ionic-framework - TypeError:无法读取未定义的属性“createHash”