首页 > 解决方案 > 如何使用 matplotlib 从 pandas 数据框列中绘制具有不同标签的子图

问题描述

我有以下代码将熊猫数据框中的列打印为两个直方图:

df = pd.read_csv('fairview_Procedure_combined.csv')

ax = df.hist(column=['precision', 'recall'], bins=25, grid=False, figsize=(12,8), color='#86bf91', zorder=2, rwidth=0.9)

ax = ax[0]
for x in ax:

    # Despine
    x.spines['right'].set_visible(False)
    x.spines['top'].set_visible(False)
    x.spines['left'].set_visible(False)

    # Switch off ticks
    x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on")

    # Draw horizontal axis lines
    vals = x.get_yticks()
    for tick in vals:
        x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)

    # Remove title
    x.set_title("")

    # Set x-axis label
    x.set_xlabel("test", labelpad=20, weight='bold', size=12)

    # Set y-axis label
    x.set_ylabel("count", labelpad=20, weight='bold', size=12)

    # Format y-axis label
    x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))

它给出了附加的输出:

在此处输入图像描述

但是,我希望在 x 轴上有不同的标签(特别是在我的column列表中列出的标签,即precisionrecall

另外,我有一个semantic_type分组by列(histsemantic_typecolor kwarg must have one color per data set. 18 data sets and 1 colors were provided

标签: pythonpandasmatplotlib

解决方案


我用子图弄清楚了……小菜一碟。


推荐阅读