Python 如何在matplotlib中绘制图形上的字符串值?

Python 如何在matplotlib中绘制图形上的字符串值?,python,pandas,dataframe,matplotlib,data-science,Python,Pandas,Dataframe,Matplotlib,Data Science,我正在做数据分割,我有1200行17列的巨大数据。我想为国家和人口的全部数据绘制图表 当我尝试使用以下代码时,我遇到了一个错误: 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 plott

我正在做数据分割,我有1200行17列的巨大数据。我想为国家和人口的全部数据绘制图表

当我尝试使用以下代码时,我遇到了一个错误:

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
的对象作为方法中的第一个参数传递,请确保字符串的格式为整数或浮点

您可以尝试:

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()    
但请注意链接文档:

警告

此函数已弃用,将在将来的版本中删除。>请调整代码以使用以下两个新功能之一:

,一个图形级函数,与绘制的绘图类型具有类似的灵活性

,一个轴级函数,用于绘制直方图,包括核密度平滑


错误太简单了。您无法使用distplot绘制分类值,您可以使用countplot进行分类功能。请显示
TestData.xls的上下文。同时请记住,该功能很快就会被弃用。你可能想切换到或。嗨,我得到下面的错误。f'对于参数“{arg_name}”预期的类型bool,接收的'ValueError:对于参数“inplace”预期的类型bool,接收的类型int.@ADSKUL是否有帮助?让我查一下@Ann Zen。