Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误matplotlib_Python_Matplotlib - Fatal编程技术网

Python 错误matplotlib

Python 错误matplotlib,python,matplotlib,Python,Matplotlib,我有数据 city inc pop edu crime cult New-York 29343,00 8683,00 0,00 10,40 0,00 Moscow 25896,00 17496,00 0,00 10,20 1,0 Rome 21785,00 15063,00 0,00 14,20 1,00 London 20000,00 70453,00 1,00 18,00 1,00 B

我有数据

city    inc pop edu crime   cult
New-York    29343,00    8683,00 0,00    10,40   0,00
Moscow  25896,00    17496,00    0,00    10,20   1,0
Rome    21785,00    15063,00    0,00    14,20   1,00
London  20000,00    70453,00    1,00    18,00   1,00
Berlin  44057,00    57398,00    1,00    6,30    1,00
我尝试构建plot并命名
plot
,并将颜色更改为columns

desire_salary = (df[(df['inc'] >= int(salary_people))])
fig = plt.figure()
result = desire_salary.pivot_table('city', 'cult', aggfunc='count').plot(kind='bar', alpha=0.75, rot=0, label="Presence / Absence of cultural centre")
plt.xlabel("Cultural centre")
plt.ylabel("Frequency")
plt.set_title('Salary and culture')
plt.plot(result[[0]], color='red')
plt.plot(result[[1]], color='blue')
plt.show()

但是它返回错误
AttributeError:“module”对象没有属性“set\u title”
TypeError:“AxesSubplot”对象没有属性“\uu getitem\uuuu”
您试图使用
AxesSubplot
类中的
set\u title
方法,但您没有使用mpl面向对象的方法。有两种方法可以纠正此问题:

  • 使用
    plt.title('Salary and culture')

  • 切换到更灵活的OO方法,并使用相关的
    axes
    方法设置标题和轴标签,例如
    ax.set\u title
    ax.set\u xlabel
    ,等等

  • 第二个错误的来源是,当您在
    pivot\u表
    上调用
    .plot
    时,返回的是matplotlib
    AxesSubplot
    对象,而不是
    数据框
    。因此,如果您尝试绘制
    result[[0]]
    ,它将尝试为
    AxesSubplot
    对象编制索引

    desire_salary = (df[(df['inc'] >= int(salary_people))])
    fig = plt.figure()
    
    # Create the pivot_table
    result = desire_salary.pivot_table('city', 'cult', aggfunc='count')
    
    # plot it in a separate step. this returns the matplotlib axes
    ax = result.plot(kind='bar', alpha=0.75, rot=0, label="Presence / Absence of cultural centre", ax=ax)
    
    ax.set_xlabel("Cultural centre")
    ax.set_ylabel("Frequency")
    ax.set_title('Salary and culture')
    
    ax.plot(result[[0]], color='red')
    ax.plot(result[[1]], color='blue')
    plt.show()
    

    要为TypeError尝试“return_type='dict'”:'AxesSubplot'对象没有属性'getitem

    Write
    plt.title(“某些标题”)
    我没有第一个错误,但第二个
    TypeError:'AxesSubplot'对象没有属性'\uu getitem'
    没有发现问题。在pivot_表上调用
    plot
    时,将返回绘制轴,而不是pivot表。我会编辑答案,这很奇怪。颜色没有改变。所有的专栏都是蓝色的当给出答案时,最好是给出一个答案。