johnchase
johnchase

Reputation: 13705

prevent overlapping bars using seaborn with pandas plotting

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)

enter image description here

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?

Upvotes: 3

Views: 2339

Answers (2)

Alexander
Alexander

Reputation: 109546

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)

enter image description here

Upvotes: 2

mwaskom
mwaskom

Reputation: 49002

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)

enter image description here

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)

enter image description here

Upvotes: 5

Related Questions