简体   繁体   English

Chaco Legend中的自定义标签

[英]Custom labels in Chaco Legend

I'd like to change the line labels on a chaco Legend because my labels need to be ascending floats: 我想更改chaco图例上的行标签,因为我的标签需要升序为浮点数:

1,2,3,4

But it is string sorting, so I'm getting: 但这是字符串排序,因此我得到:

1, 10, 11, 2, 21  etc...

I noticed the documentation seems unfinished in regard to this: 我注意到有关此文件的文档似乎尚未完成:

http://chaco.readthedocs.org/en/latest/user_manual/basic_elements/overlays.html#legend http://chaco.readthedocs.org/en/latest/user_manual/basic_elements/overlays.html#legend

I've tried setting the legends labels manually: 我尝试过手动设置图例标签:

self.plot.legend.labels = list([i for i in self.mylist]) 

I'm using a colormap, so this is very noticeable as the legend shows blue lines and red lines mixed seemingly randomly due to the string sorting. 我正在使用一个颜色图,因此这非常引人注目,因为图例显示由于字符串排序,蓝线和红线似乎随机混合。

Below is a minimal working example 以下是一个最小的工作示例

This example does not use the same colormap I'm using, but shows how the line ordering in the legend is not sorted. 本示例未使用与我使用的相同的颜色图,但显示了图例中的行顺序是如何排序的。 It's not important which colormap is used, what's important is the string sorting in the legend gives unwanted aesthetics. 使用哪种颜色图并不重要,图例中的字符串排序会带来不必要的美感,这一点很重要。

from traits.api import *
from chaco.api import *
from traitsui.api import *
from chaco.example_support import COLOR_PALETTE
from enable.api import ComponentEditor
import numpy as np


class TestPlot(HasTraits):

    plot = Instance(Plot)

    traits_view = View( Item('plot', editor=ComponentEditor(), show_label=False) )

    def _plot_default(self):
        data = ArrayPlotData()
        plot = Plot(data)

        x = np.linspace(0,10,100)
        data.set_data('x', x)

        for i, freq in enumerate(range(1,20,3)):
            y = 'line_%s' % freq
            color = tuple(COLOR_PALETTE[i])
            data.set_data(y, i*x)
            plot.plot(('x', y), name=y, color=color)

        plot.legend.visible = True


        return plot


if __name__ == '__main__':
    TestPlot().configure_traits()

See screenshot: 看截图:

在此处输入图片说明

To sort your labels properly you need just to apply natural sorting. 要正确分类标签,您只需要应用自然分类即可。 Install "natsort" library and insert two lines in your code: 安装“ natsort”库,并在代码中插入两行:

from natsort import natsorted
...
plot.legend.labels = natsorted(plot.plots.keys())

This will do the trick. 这将达到目的。

You can add leading zeros for one digit numbers by changing the line 您可以通过更改行为一位数字添加前导零

y = 'line_%s' % freq 

to

y = 'line_%02d' % freq

I assume you have no more than 99 graphs otherwise you need to change the 02 to 03 . 我假设您的图表不超过99个,否则您需要将02更改为03 Then your legend should be correctly sorted. 然后,您的图例应正确排序。

See https://docs.python.org/3.4/library/string.html#format-specification-mini-language for more information on the string format specifiers. 有关字符串格式说明符的更多信息,请参见https://docs.python.org/3.4/library/string.html#format-specification-mini-language

The format 0# where # is a number, means that in the string the number uses # positions and if the number is smaller than the given width it is filled with trailing zeros. 格式为0# ,其中#是数字,表示在字符串中,数字使用#位置,如果数字小于给定的宽度,则以尾随零填充。 If you want floating point numbers with one digit as fractional part use %04.1f 如果要使用一位数字作为小数部分的浮点数,请使用%04.1f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM