[英]How to plot the string values on the graph in matplotlib?
我正在做数据分割,我有 1200 行和 17 列的大量数据。 我想要 plot 国家和人口的整个数据的图表。
当我尝试使用以下代码时,出现错误:
ValueError: could not convert string to float: 'Canada'
编码:
import pandas as pd # for dataframes
import matplotlib.pyplot as plt # for plotting graphs
import seaborn as sns # for plotting graphs
import datetime as dt
data = pd.read_excel("TestData.xls")
plt.figure(1, figsize=(15, 6))
n=0
for x in ['Country', 'Product', 'Sales']:
n += 1
plt.subplot(1,3,n)
plt.subplots_adjust(hspace=0.5, wspace=0.5)
sns.distplot(data[x], bins=20)
plt.title('Displot of {}'.format(x))
plt.show()
如果您将str
类型的 object 作为seaborn.distplot()
方法中的第一个参数传递,请确保字符串的格式为 Z157DB7DF530023575515D366C9B672。
你可以试试:
import pandas as pd # for dataframes
import matplotlib.pyplot as plt # for plotting graphs
import seaborn as sns # for plotting graphs
import datetime as dt
data = pd.read_excel("TestData.xls")
plt.figure(1, figsize=(15, 6))
n=0
for x in ['Country', 'Product', 'Sales']:
n += 1
plt.subplot(1,3,n)
plt.subplots_adjust(hspace=0.5, wspace=0.5)
a = data[x]
if a.replace('.', '', 1).isdigit():
sns.distplot(a, bins=20)
else:
print(f"{a} is not a float.")
plt.title('Displot of {}'.format(x))
plt.show()
但请注意链接文档:
警告
此 function 已弃用,将在未来版本中删除。 >请调整您的代码以使用以下两个新功能之一:
displot()
,一个图形级别的 function 具有与 plot 类似的灵活性来绘制
histplot()
,一个轴级 function 用于绘制直方图,包括 kernel 密度平滑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.