prevent overlapping bars using seaborn with pandas plotting

☆樱花仙子☆ 提交于 2019-12-10 22:24:18

问题


I am trying to use pandas plotting to create a stacked horizontal barplot with a seaborn import. I would like to remove space between the bars, but also not have the bars overlap. This is what I've tried:

import pandas as pd
import numpy as pd
import seaborn as sns

df = pd.DataFrame(np.random.rand(15, 3))
df.plot.barh(stacked=True, width=1)

This seems to work without importing seaborn, though I like the seaborn style and it is usually an import in the ipython notebook I am working in is this possible?


回答1:


This artifact is also visible with matplotlib defaults if you set the bar linewidth to what seaborn style has:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(15, 3))
df.plot(stacked=True, width=1, kind="barh", lw=.5)

A solution would be to increase the bar lines back to roughly where the matplotlib defaults are:

import pandas as pd
import numpy as np
import seaborn as sns

df = pd.DataFrame(np.random.rand(15, 3))
df.plot(stacked=True, width=1, kind="barh", lw=1)




回答2:


Perhaps you should reduce the line width?

import seaborn as sns

f, ax = plt.subplots(figsize=(10, 10))
df.plot(kind='barh', stacked=True, width=1, lw=0.1, ax=ax)



来源:https://stackoverflow.com/questions/36185427/prevent-overlapping-bars-using-seaborn-with-pandas-plotting

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