How can I fix the issue based on showing image in each horizontal bar in subplot area in Python?

人走茶凉 提交于 2021-02-11 12:50:14

问题


I have a problem about showing image in each bar of horizontal bar plot in subplot(2 row , 2 column) in Python.

My aim is to devise a subplot based on 2 row and 2 column and then draw horizontal bar related with dataframe with each image putting in each bar.

Image format is "png".

I wrote a code snippet shown below but it didn't work. Only one bar with related with appeared in each horizontal bar.

f,a = plt.subplots(2,2,figsize=(18,14))
f.subplots_adjust(wspace = 0.5, hspace=0.4)

colors_1 = random.choices(list(mcolors.CSS4_COLORS.values()),k = df_top_5_player_age.size)
colors_2 = random.choices(list(mcolors.CSS4_COLORS.values()),k = df_top_5_player_value.size)
colors_3 = random.choices(list(mcolors.CSS4_COLORS.values()),k = df_top_5_player_height.size)
colors_4 = random.choices(list(mcolors.CSS4_COLORS.values()),k = df_top_5_player_weight.size)

plt.style.use('seaborn')

#### AGE
X = [p for p in df_top_5_player_age["Age"]]
Y = [p for p in df_top_5_player_age["Name"]]

liste_pays_1 = [ (i,y) for i,y in zip(df_top_5_player_age["Photo"],df_top_5_player_age["Age"])]

height = 0.9
a[0,0].barh(y=Y, width=X, height=height, color=colors_1, align='center')
a[0,0].set_title('Top 5 Player by Age', fontsize=15, fontweight='bold')

for i, (label, value) in enumerate(liste_pays_1):
    r = requests.get(label,
                  stream=True, headers={'User-agent': 'Mozilla/5.0'})
    img = mpimg.imread(r.raw ,format="png")
    a[0,0].imshow(img, extent=[value - 8, value - 2, i - height / 2, i + height / 2], aspect='auto', zorder=2)
    
#### VALUE
X = [p for p in df_top_5_player_value["Value"]]
Y = [p for p in df_top_5_player_value["Name"]]

liste_pays_2 = [ (i,y) for i,y in zip(df_top_5_player_value["Photo"],df_top_5_player_value["Value"])]


height = 0.9
a[0,1].barh(y=Y, width=X, height=height, color=colors_2, align='center')
a[0,1].set_title('Top 5 Player by Value', fontsize=15, fontweight='bold')

for i, (label, value) in enumerate(liste_pays_2):
    r = requests.get(label,
                  stream=True, headers={'User-agent': 'Mozilla/5.0'})
    img = mpimg.imread(r.raw)
    a[0,1].imshow(img, extent=[value - 8, value - 2, i - height / 2, i + height / 2], aspect='auto', zorder=2)    
    
#### HEIGHT
X = [p for p in df_top_5_player_height["Height"]]
Y = [p for p in df_top_5_player_height["Name"]]

liste_pays_3 = [ (i,y) for i,y in zip(df_top_5_player_height["Photo"],df_top_5_player_height["Height"])]


height = 0.9
a[1,0].barh(y=Y, width=X, height=height, color=colors_2, align='center')
a[1,0].set_title('Top 5 Player by Height', fontsize=15, fontweight='bold')

for i, (label, value) in enumerate(liste_pays_3):
    r = requests.get(label,
                  stream=True, headers={'User-agent': 'Mozilla/5.0'})
    img = mpimg.imread(r.raw)
    a[1,0].imshow(img, extent=[value - 8, value - 2, i - height / 2, i + height / 2], aspect='auto', zorder=2)   


#### HEIGHT
X = [p for p in df_top_5_player_weight["Weight"]]
Y = [p for p in df_top_5_player_weight["Name"]]

liste_pays_4 = [ (i,y) for i,y in zip(df_top_5_player_weight["Photo"],df_top_5_player_weight["Weight"])]

height = 0.9
a[1,1].barh(y=Y, width=X, height=height, color=colors_2, align='center')
a[1,1].set_title('Top 5 Player by Weight', fontsize=15, fontweight='bold')

for i, (label, value) in enumerate(liste_pays_4):
    r = requests.get(label,
                  stream=True, headers={'User-agent': 'Mozilla/5.0'})
    img = mpimg.imread(r.raw)
    a[1,1].imshow(img, extent=[value - 8, value - 2, i - height / 2, i + height / 2], aspect='auto', zorder=2)  

plt.tight_layout()
plt.show() 

How can I fix it?

Here is the code shown below.

来源:https://stackoverflow.com/questions/64505507/how-can-i-fix-the-issue-based-on-showing-image-in-each-horizontal-bar-in-subplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!