Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn barplot with rounded corners

I'm trying to plot some bars but would like to control the roundness of the corners. I tried following the answer provided in stack question Bar chart with rounded corners in Matplotlib but can't seem to get the same result. How can I get the bars to have rounded edges with control to the roundness? Also is there a better alternative FancyBboxPatch?

Below is my testable code along with the current and desired output.

My Code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import FancyBboxPatch

mydict = {
    'Event': ['Running', 'Swimming', 'Biking', 'Hiking'],
    'Completed': [2, 4, 3, 7],
    'Participants': [10, 20, 35, 10]}

df = pd.DataFrame(mydict).set_index('Event')
df = df.assign(Completion=(df.Completed / df.Participants) * 100)
print(df)

plt.subplots(figsize=(5, 2))

sns.set_color_codes("pastel")
ax = sns.barplot(x=df.Completion, y=df.index, orient='h', joinstyle='bevel')

new_patches = []
for patch in reversed(ax.patches):
    bb = patch.get_bbox()
    color = patch.get_facecolor()
    p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
                            abs(bb.width), abs(bb.height),
                            boxstyle="round,pad=-0.0040,rounding_size=0.015",
                            ec="none", fc=color,
                            mutation_aspect=4
                            )
    patch.remove()
    new_patches.append(p_bbox)

for patch in new_patches:
    ax.add_patch(patch)

sns.despine(left=True, bottom=True)
ax.tick_params(axis=u'both', which=u'both', length=0)
plt.tight_layout()
plt.show()

Example DataFrame:

          Completed  Participants  Completion
Event                                        
Running           2            10   20.000000
Swimming          4            20   20.000000
Biking            3            35    8.571429
Hiking            7            10   70.000000

Current Output:

enter image description here

Desired Output:

enter image description here

like image 507
MBasith Avatar asked May 03 '20 02:05

MBasith


1 Answers

Just play a little with the parameters mutation_aspect and rounding_size, keep in mind that the dimensions of your data are different. Check BoxStyle and FancyBboxPatch for more information.

Example with mutation_aspect==0.2 and rounding_size=2

plt.subplots(figsize=(5, 2))
sns.set_color_codes("pastel")
ax = sns.barplot(x=df.Completion, y=df.index, joinstyle='bevel')

new_patches = []
for patch in reversed(ax.patches):
    # print(bb.xmin, bb.ymin,abs(bb.width), abs(bb.height))
    bb = patch.get_bbox()
    color = patch.get_facecolor()
    p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
                            abs(bb.width), abs(bb.height),
                            boxstyle="round,pad=-0.0040,rounding_size=2",
                            ec="none", fc=color,
                            mutation_aspect=0.2
                            )
    patch.remove()
    new_patches.append(p_bbox)

for patch in new_patches:
    ax.add_patch(patch)

sns.despine(left=True, bottom=True)

ax.tick_params(axis=u'both', which=u'both', length=0)
plt.tight_layout()
# plt.savefig("data.png", bbox_inches="tight")
plt.show()

enter image description here

like image 106
jcaliz Avatar answered Nov 06 '22 19:11

jcaliz