我真的很难理解如何建立查科酒吧的地块。
我一直在研究一个在线示例,并将其简化为以下简化代码:
import numpy as np
from traits.api import HasTraits, Instance
from traitsui.api import View, Item
from chaco.api import BarPlot, ArrayDataSource, DataRange1D, LinearMapper
from enable.api import ComponentEditor
class MyBarPlot(HasTraits):
plot = Instance(BarPlot)
traits_view = View(
Item('plot',editor=ComponentEditor(), show_label=False))
def _plot_default(self):
idx = np.array([1, 2, 3, 4, 5])
vals = np.array([2, 4, 7, 4, 3])
index = ArrayDataSource(idx)
index_range = DataRange1D(index, low=0.5, high=5.5)
index_mapper = LinearMapper(range=index_range)
value = ArrayDataSource(vals)
value_range = DataRange1D(value, low=0)
value_mapper = LinearMapper(range=value_range)
plot = BarPlot(index=index, value=value,
value_mapper=value_mapper,
index_mapper=index_mapper)
return plot
if __name__ == "__main__":
myplot = MyBarPlot()
myplot.configure_traits()不用说,我的尝试没有奏效。当我运行这段代码(在一个ipython笔记本中)时,我得到的只是一个空白的绘图窗口,充满了黑色。
我怀疑我的错误可能与“valuemapper”条目有关,因为我并不真正理解这些条目的作用。如果有任何关于我的代码错误的提示,我将不胜感激。
更一般说来,这种编码方法对我来说非常复杂--是否有一种更简单的方法来制作查科酒吧的图样?
发布于 2015-02-05 04:07:19
不幸的是,bar_width的BarPlot特性并没有试图在这里变得聪明。默认情况下,数据空间中的宽度为10,这完全超过了绘图的宽度。要解决这个问题,您可以手动调整宽度:
plot = BarPlot(index=index, value=value,
value_mapper=value_mapper,
index_mapper=index_mapper,
bar_width=0.8)请注意,这仍然不会给您一个正确的“条形图”,因为您将丢失周围的轴装饰和其他组件。要做到这一点,最简单的方法就是使用查科的Plot对象。
下面是一个将BarPlot实例添加到Plot中的示例
https://github.com/enthought/chaco/blob/master/examples/demo/tornado.py
下面是一个使用plot方便方法在Plot中创建条形图的示例
是的,“阴谋”是非常可怕的过载(Chaco - Getting multiple data series to use the same axes and maps)。
https://stackoverflow.com/questions/28334842
复制相似问题