-3

Is there a way to keep images as axis values? Two similar questions here and here does not answer my question.

import seaborn as sns
import matplotlib.pyplot as plt
titanic = sns.load_dataset("titanic")
sns.catplot(x="sex", y="survived", hue="class", kind="bar", data=titanic)

I would like to replace the male and female axis values with the corresponding image present in the image link. Can we map the axis values to the image links?

enter image description here

Male:
https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSuGDLqvyU56RbTEFQP3ohzx9d0vJv-nQOk1g&usqp=CAU

Female:
https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRnSlVrt4o9yHIHnJ7H-cPi_fhOC4bePnyOoA&usqp=CAU

Rajesh
  • 766
  • 5
  • 17
  • 2
    Very unclear why those questions don't answer your question. They look like exactly what you're trying to do. Did you try to implement the approach? – BigBen Oct 21 '20 at 19:29

1 Answers1

2

The answer using an OffsetBox in the questions you linked is probably the best option

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnchoredOffsetbox

titanic = sns.load_dataset("titanic")
images = ["https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSuGDLqvyU56RbTEFQP3ohzx9d0vJv-nQOk1g&usqp=CAU",
          "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRnSlVrt4o9yHIHnJ7H-cPi_fhOC4bePnyOoA&usqp=CAU"]
pos = [0,1]

fig, ax = plt.subplots()
ax = sns.barplot(x="sex", y="survived", hue="class", data=titanic)
ax.set_xticklabels([])
for m,p in zip(images,pos):
    image = plt.imread(m)
    im = OffsetImage(image, zoom=0.1)
    ab = AnchoredOffsetbox(loc='upper center', child=im, 
                           bbox_to_anchor=(p,0), bbox_transform=ax.get_xaxis_transform(),
                           frameon=False)
    ax.add_artist(ab)

plt.tight_layout()

enter image description here

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75