Python Windrose图例标签

2024-04-28 03:34:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图完全控制Windrose中的图例;该图例由bin控制,可以使用ax.info['bins']调用。但是,我想更改bin组的名称。在

Bin group names

在网上查找时,我找到的唯一解决方案是编辑windrose的.py文件,对其进行编辑,然后将其重新导入到python脚本中。在

Python Windrose legend bracket format and loc

我在寻找一个更简单的方法,因为我计划在脚本中为不同的情节设置不同的箱子。到目前为止,我已经尝试了label和{},但似乎不起作用。在


Tags: 文件pyinfo脚本名称编辑bin解决方案
1条回答
网友
1楼 · 发布于 2024-04-28 03:34:06

通过在中添加labels参数ax.set_图例并更改脚本以接受参数。我还添加了一个小验证,以防标签与句柄不匹配时显示错误。在

ax.set_legend(labels = ['T', 'S', 'M'], title="Failure Mode", loc="upper left")

    def get_labels(labels, decimal_places=1):
        _decimal_places = str(decimal_places)

        fmt = (
            "[%." + _decimal_places + "f " +
            ": %0." + _decimal_places + "f"
        )

        if labels is None:
            labels = np.copy(self._info['bins'])
            if locale.getlocale()[0] in ['fr_FR']:
                fmt += '['
            else:
                fmt += ')'
            labels = [fmt % (labels[i], labels[i + 1])
                for i in range(len(labels) - 1)]
        else:
            if len(labels) != len(self._info['bins']) -1 :
                print("ERROR")
            labels = labels

        return labels


    kwargs.pop('handles', None)

    decimal_places = kwargs.pop('decimal_places', 1)
    labels = kwargs.pop('labels', None)

    handles = get_handles()
    labels = get_labels(labels, decimal_places)
    self.legend_ = mpl.legend.Legend(self, handles, labels, loc, **kwargs)
    return self.legend_

更多信息:Github issue solution

相关问题 更多 >