Is there any way to get just the mini boxplot from a seaborn violinplot?

孤街浪徒 提交于 2019-12-11 19:06:36

问题


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:



来源:https://stackoverflow.com/questions/52971882/is-there-any-way-to-get-just-the-mini-boxplot-from-a-seaborn-violinplot

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