我将不胜感激。我以前可以使用此代码获取图像,但回顾我的工作,现在却给了我这个错误。有什么想法可以解决这个问题吗?也许这是由于最近升级了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'
答案 0 :(得分:1)
我能够解决。显然,您无法在matplotlib 2.2中使用数据框调用matplotlib.colors.Normalize。使用这些值,我将第4行更新为
colors['intensity'] = norm(((year_means-y) / year_std).values)