![](/img/trans.png)
[英]AttributeError: 'str' object has no attribute 'pop' in matplotlib
[英]AttributeError: 'str' object has no attribute 'add_artist' matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage,AnnotationBbox
def get_flag(name):
path = "flags/flags/{}.png".format(name)
im = plt.imread(path)
return im
def offset_image(name, ax, xy):
img = get_flag(name)
im = OffsetImage(img, zoom=0.72)
im.image.axes = ax
ab = AnnotationBbox(im, xy, frameon=False, xycoords='data', boxcoords="offset points", pad=0)
ax.add_artist(ab)
ax = df.plot(kind='scatter',x='% of Ages 65+',y='COVID Fatality Rate', title='Correlation between % of Ages 65+ and COVID Fatality Rate')
for i, c in enumerate(df['Country']):
offset_image(i, c, ax)
plt.show()
我在散布 plot 上注釋國旗而不是標記。 但是 AttributeError: 'str' object has no attribute 'add_artist' 出現。 我該如何解決?
您有兩個與您的offset_image()
相關的錯誤。
首先,正如 ewong 和 Phani Rithvij 在評論中指出的那樣,您在offset_image()
中定義的參數順序和您調用它的順序是不一樣的。
其次, AnnotationBbox中的xy參數需要是一個元組或一個列表,表示坐標系中的一個點。 但是,您只傳遞一個值c
。
在不更改定義offset_image()
的方式的情況下,您只需更改調用方式,如下所示:
for i, c in enumerate(df['Country']):
offset_image(i, ax, (i, c))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.