热线电话:13121318867

登录
2019-03-06 阅读量: 744
带有pandas csv的Historgram

我有这样的数据集:

num gb bw

2.2 green black

3.3 red white

2.2 green black

11.0 red black

etc

我试图想出一个包含4个直方图的子图,其中每个直方图都是一种颜色的数据。

这是我的代码:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

df = pd.read_csv('hw03_problem2.csv' , skiprows=1, names = ['num', 'gb', 'bw'])

#df = df.astype(float)

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')

df[df.gb == 'green'].num.plot.hist(ax = ax1)

df[df.gb == 'red'].num.plot.hist(ax = ax2)

df[df.bw == 'black'].num.plot.hist(ax = ax3)

df[df.bw == 'white'].num.plot.hist(ax = ax4)

plt.show()

plt.savefig('subplot_for_q2')

我收到此错误:

Empty 'DataFrame': no numeric data to plot

解决办法:问题可能是“num”列格式。在绘图之前将其转换为浮动。

就像是:

df.num = df.num.astype(float)

6.4876
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子