Chaco Legend 中的自定义标签
Custom labels in Chaco Legend
我想更改 chaco Legend 上的线条标签,因为我的标签需要升序浮动:
1,2,3,4
但它是字符串排序,所以我得到:
1, 10, 11, 2, 21 etc...
我注意到关于这个的文档似乎还未完成:
http://chaco.readthedocs.org/en/latest/user_manual/basic_elements/overlays.html#legend
我试过手动设置图例标签:
self.plot.legend.labels = list([i for i in self.mylist])
我使用的是色图,所以这非常明显,因为图例显示的蓝线和红线由于字符串排序而看似随机混合。
下面是一个最小的工作示例
此示例未使用我正在使用的相同颜色图,但显示了图例中的行顺序未排序的方式。使用哪个颜色图并不重要,重要的是图例中的字符串排序给出了不想要的美感。
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()
见截图:
您可以通过更改行
为一位数字添加前导零
y = 'line_%s' % freq
到
y = 'line_%02d' % freq
我假设您的图表不超过 99 个,否则您需要将 02
更改为 03
。那么您的图例应该正确排序。
有关字符串格式说明符的详细信息,请参阅 https://docs.python.org/3.4/library/string.html#format-specification-mini-language。
格式0#
,其中#
是一个数字,表示在字符串中数字使用#
个位置,如果数字小于给定的宽度,则填充尾随零。如果您想要将一位数字作为小数部分的浮点数,请使用 %04.1f
要正确排序标签,您只需应用自然排序。安装 "natsort" 库并在代码中插入两行:
from natsort import natsorted
...
plot.legend.labels = natsorted(plot.plots.keys())
这样就可以了。
我想更改 chaco Legend 上的线条标签,因为我的标签需要升序浮动:
1,2,3,4
但它是字符串排序,所以我得到:
1, 10, 11, 2, 21 etc...
我注意到关于这个的文档似乎还未完成:
http://chaco.readthedocs.org/en/latest/user_manual/basic_elements/overlays.html#legend
我试过手动设置图例标签:
self.plot.legend.labels = list([i for i in self.mylist])
我使用的是色图,所以这非常明显,因为图例显示的蓝线和红线由于字符串排序而看似随机混合。
下面是一个最小的工作示例
此示例未使用我正在使用的相同颜色图,但显示了图例中的行顺序未排序的方式。使用哪个颜色图并不重要,重要的是图例中的字符串排序给出了不想要的美感。
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()
见截图:
您可以通过更改行
为一位数字添加前导零y = 'line_%s' % freq
到
y = 'line_%02d' % freq
我假设您的图表不超过 99 个,否则您需要将 02
更改为 03
。那么您的图例应该正确排序。
有关字符串格式说明符的详细信息,请参阅 https://docs.python.org/3.4/library/string.html#format-specification-mini-language。
格式0#
,其中#
是一个数字,表示在字符串中数字使用#
个位置,如果数字小于给定的宽度,则填充尾随零。如果您想要将一位数字作为小数部分的浮点数,请使用 %04.1f
要正确排序标签,您只需应用自然排序。安装 "natsort" 库并在代码中插入两行:
from natsort import natsorted
...
plot.legend.labels = natsorted(plot.plots.keys())
这样就可以了。