我有问题从熊猫系列对象制作直方图,我不明白为什么它不起作用.代码之前运行良好,但现在却没有.
这是我的一些代码(具体来说,我正在尝试制作直方图的pandas系列对象):
type(dfj2_MARKET1['VSPD2_perc'])
Run Code Online (Sandbox Code Playgroud)
输出结果:
pandas.core.series.Series
这是我的绘图代码:
fig, axes = plt.subplots(1, 7, figsize=(30,4))
axes[0].hist(dfj2_MARKET1['VSPD1_perc'],alpha=0.9, color='blue')
axes[0].grid(True)
axes[0].set_title(MARKET1 + ' 5-40 km / h')
Run Code Online (Sandbox Code Playgroud)
错误信息:
AttributeError Traceback (most recent call last)
<ipython-input-75-3810c361db30> in <module>()
1 fig, axes = plt.subplots(1, 7, figsize=(30,4))
2
----> 3 axes[1].hist(dfj2_MARKET1['VSPD2_perc'],alpha=0.9, color='blue')
4 axes[1].grid(True)
5 axes[1].set_xlabel('Time spent [%]')
C:\Python27\lib\site-packages\matplotlib\axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
8322 # this will automatically overwrite bins,
8323 # so that …Run Code Online (Sandbox Code Playgroud) 我想创建一个堆叠直方图.如果我有一个由三个等长数据集组成的二维数组,这很简单.代码和图片如下:
import numpy as np
from matplotlib import pyplot as plt
# create 3 data sets with 1,000 samples
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(1000,3)
#Stack the data
plt.figure()
n, bins, patches = plt.hist(x, 30, stacked=True, normed = True)
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用具有不同长度的三个数据集的类似代码,则结果是一个直方图覆盖另一个直方图.有什么办法可以用混合长度数据集进行叠加直方图吗?
##Continued from above
###Now as three separate arrays
x1 = mu + sigma*np.random.randn(990,1)
x2 = mu + sigma*np.random.randn(980,1)
x3 = mu + sigma*np.random.randn(1000,1)
#Stack the data
plt.figure()
plt.hist(x1, bins, stacked=True, normed = True)
plt.hist(x2, bins, …Run Code Online (Sandbox Code Playgroud)