Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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气泡图Legands-TypeError_Python_R_Pandas_Dataframe_Matplotlib - Fatal编程技术网

Python气泡图Legands-TypeError

Python气泡图Legands-TypeError,python,r,pandas,dataframe,matplotlib,Python,R,Pandas,Dataframe,Matplotlib,这是我的密码: import pandas import matplotlib.pyplot as plt import matplotlib.patches as mpatches %matplotlib inline pandas.set_option ('max_columns',10) df= pandas.read_csv('C:/Users/HP/Desktop/Coding/Python/2.Python Data Analysis/Module 2- Python Data

这是我的密码:

import pandas
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

%matplotlib inline

pandas.set_option ('max_columns',10)

df= pandas.read_csv('C:/Users/HP/Desktop/Coding/Python/2.Python Data Analysis/Module 2- Python Data Visualization/M2-Bubble Chart with Labels and Legend data.csv')

plt.scatter(x=df['GDP per Capita'],y=df['Life Span'],s=df['Population']/1000, alpha=0.5, c=df['Bubble color'])
            #alpha at 0.5 to set the transparency 



#chart title, axis labels 
plt.title('GDP,Lifespan,Population (Bubble size)')
plt.xlabel(' GDP')
plt.ylabel('Lifespan')

#bubble labels 
x,y= df['GDP per Capita'],df['Life Span']
for i, txt in enumerate (df['Territory']):
    plt.annotate(txt,(x[i],y[i]))
    print(i,txt,x[i],y[i],df['Population'][i],df['Bubble color'][i])
#annotate: is used to assign the population with the chart sp have from text, the x and y will be the next one 
#it will print out the index number with the one that assigned with it as well 


#legend 
territory_list=list(df['Territory'])
bubble_color_list = list(df['Bubble color'])
l = []
for i in range (0,len(df.index)):
        l.append(mpatches.Patch(color=bubble_color_list[i],
                               alpha=0.5,
                               label=territory_list[i]))





plt.legend(handles=1,loc=(2,0))   
#the i is in the For loop is just basically like the one above to have all the information
我希望生成一个带有图例的气泡图,但不知何故,它并不像它想象的那样显示图例,只是显示图表,然后显示此消息

`TypeError: 'int' object is not iterable`

我做错了什么?

plt.legend(handles=1,loc=(2,0))
中不能有
handles=1

句柄
必须是容器,例如列表、元组等

不仅如此,一个包含整数的容器也是不可接受的。不要写入
句柄=[1,2,3]

以下代码显示了如何正确调用
legend
方法:

import numpy as np
import matplotlib.pyplot as plt

# Make some fake data.
a = b = np.arange(0, 3, .02)
c = np.exp(a)
d = c[::-1]


fig, ax = plt.subplots()
line1 = ax.plot(a, c, 'k--')
line2 = ax.plot(a, d, 'k:')
line3 = ax.plot(a, c + d, 'k')

ax.legend((line1, line2, line3), ('line 1', 'line 2', 'line 3'), loc=(2,0))

plt.show()

哪一行正在生成错误?嘿@aprilangel,它的编号是43--->43 plt。图例(handles=1,loc=(2,0))看起来handles将接受iterables,至少您应该放置[1]。请参见此处的文档: