我想更改 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()
看截图: