Is there any way to get just the mini boxplot from

2019-09-02 15:35发布

问题:

I'm trying to draw a very long series of boxplots. I like the aesthetic of the miniature boxplots drawn inside violinplot (controlled via the "inner" parameter to seaborn.violinplot). Does anyone know of an easy way to draw just this mini boxplot without the rest of the violinplot? Thanks!

回答1:

The violins are PolyCollection objects. You could remove all PolyCollections from the axes. This would make sense if the axes only contain the violin plots and not any other PolyCollections in addition.

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", data=tips)


from matplotlib.collections import PolyCollection
for a in ax.findobj(PolyCollection):
    a.remove()

ax.relim()
ax.autoscale_view()

plt.show()

Or even simpler,

for a in ax.collections:
    a.remove()


回答2:

I was able to create a subclass of violinplot that does what I want. Basically I just copied the code that draws the voilins and deleted the parts I didn't need. This is obviously a little ugly but it did the job. If anyone else comes up with a more elegant solution please post your answer.

You can find my solution at: https://gist.github.com/mdbecker/c21e6a8a6ce893b61eecd880d9f18a83

Which produces results like: