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 PolyCollection
s from the axes. This would make sense if the axes only contain the violin plots and not any other PolyCollection
s 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: