繁体   English   中英

在 google colab 上绘制 csv 文件的直方图的问题

[英]Issues plotting a histogram of a csv file on google colab

我是 google colab 的新手,我正在尝试使用 matplotlib 生成 plot csv 文件的直方图,但出现错误。

此代码能够读取和显示我的数据

import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import pylab as pl

df = pd.read_csv('tree_result.csv')
df

但是当我尝试 plot 一个包含数据中两个字段的直方图时,我得到一个错误

fig, ax = plt.subplots(figsize = (50,10))
x = df['spc_common']
y = df['count']
plt.bar(x, height=y,align = 'center', width = 0.8)
plt.xlabel('Name of Trees (common name)', size = 10)
plt.ylabel('Number of Trees', size = 10)
pl.xticks(rotation = 90)
plt.show()

错误信息

TypeError: 'value' must be an instance of str or bytes, not a float

为了解决这个问题,我将数据类型更改为字符串。 这解决了它的问题

fig, ax = plt.subplots(figsize = (50,10))
x = df['spc_common'].astype(str)
y = df['count']
plt.bar(x, height=y,align = 'center', width = 0.8)
plt.xlabel('Name of Trees (common name)', size = 10)
plt.ylabel('Number of Trees', size = 10)
pl.xticks(rotation = 90)
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM