【问题标题】:Seaborn barplot with rounded corners带有圆角的 Seaborn 条形图
【发布时间】:2020-08-17 12:06:16
【问题描述】:

我正在尝试绘制一些条形图,但想控制角的圆度。我尝试按照堆栈问题Bar chart with rounded corners in Matplotlib 中提供的答案进行操作,但似乎无法得到相同的结果。如何让条形边缘具有圆角并控制圆度?还有更好的 FancyBboxPatch 替代品吗?

以下是我的可测试代码以及​​当前和所需的输出。

我的代码:

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()

示例数据框:

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

电流输出:

所需的输出:

【问题讨论】:

    标签: python-3.x pandas matplotlib seaborn


    【解决方案1】:

    稍微玩一下参数mutation_aspectrounding_size,请记住,您的数据的维度是不同的。查看BoxStyleFancyBboxPatch 了解更多信息。

    mutation_aspect==0.2rounding_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()
    

    【讨论】:

    • 这很酷。感谢您对 Q 的提示和帮助。这完成了我想要的。将检查提供的参考资料以获取更多信息。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 2016-08-06
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多