Python 类型错误 : float() argument must be a string or a number, 而不是 'SingleBlockManager'

标签 python matplotlib

如有任何意见,我将不胜感激。我以前能够使用此代码获取图像,但回顾我的工作,现在它给了我这个错误。我有什么想法可以解决这个问题吗?也许这是由于最近升级了 matplotlib?

我正在使用的代码

import pandas as pd
import numpy as np
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(32000,200000,3650), 
                   np.random.normal(43000,100000,3650), 
                   np.random.normal(43500,140000,3650), 
                   np.random.normal(48000,70000,3650)], 
                  index=[1992,1993,1994,1995])
import matplotlib as mpl
import matplotlib.pyplot as plt
import scipy.stats as ss
%matplotlib notebook
n = df.shape[1]
year_means = df.mean(axis=1)
year_std = df.std(axis=1)/(np.sqrt(n))
yerr = year_std * 1.96
y= 37000
norm = mpl.colors.Normalize(vmin=-1.96,vmax=1.96)
cmap = mpl.cm.get_cmap('seismic')
colors = pd.DataFrame([])
colors['intensity'] = norm((year_means-y) / year_std)
colors['color'] = [cmap(x) for x in colors['intensity']]
plt.figure()
bar_plot = plt.bar(range(df.shape[0]), year_means, yerr = yerr, color = colors['color']);
hoz_line = plt.axhline(y=y, color='grey', linewidth=2, linestyle = ':');
y_text = plt.text(3.4, y, 'y = %d' %y, bbox=dict(fc='white',ec='k'));
plt.xticks(range(df.shape[0]), df.index, alpha = 0.8);

我收到错误消息

TypeError                                 Traceback (most recent call last)
<ipython-input-10-6b68c0797acf> in <module>()
      2 cmap = mpl.cm.get_cmap('seismic')
      3 colors = pd.DataFrame([])
----> 4 colors['intensity'] = norm((year_means-y) / year_std)
      5 colors['color'] = [cmap(x) for x in colors['intensity']]
      6 plt.figure()

TypeError: float() argument must be a string or a number, not 'SingleBlockManager'

最佳答案

我能够修复。显然,在 matplotlib 2.2 中您无法再使用数据帧调用 matplotlib.colors.Normalize 。使用这些值,我将第 4 行更新为

colors['intensity'] = norm(((year_means-y) / year_std).values)

关于Python 类型错误 : float() argument must be a string or a number, 而不是 'SingleBlockManager',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678065/

相关文章:

python - 为什么在 Python 3.x 中的循环中使用 list() 会返回内存错误?

python - 如何从列表中删除重复的单词?

python - 使用 Matplotlib、PyQt 和 Threading 进行实时绘图以 python 崩溃结束

python - 如何在Python中使用tesseract ocr获取结构形式的信息?

python - AttributeError at/accounts/signup/'Person' 对象没有属性 'META'

java - 可以用Java实现twisted吗?

python - 添加以列出类实例

python - 无法使用plt.imshow显示图片

python - Matplotlib Pandas 日期时间频率

python - 如何在ipython笔记本中设置matplotlib图形默认大小?