Мелкие галочки в сюжете Чако?

Незначительные галочки в Чако всегда опускаются: введите здесь описание изображения

Это не всегда удобно. Возможно ли иметь незначительные галочки в Chaco, как в matplotlib:

введите здесь описание изображения

Не нашел ничего связанного .. Спасибо.


person funnyp0ny    schedule 12.01.2016    source источник


Ответы (1)


Изменить: эта функция теперь добавлена ​​в Chaco 4.6, поэтому, если вы используете эту версию или более позднюю, используйте следующий аналогичный код. Если нет, см. исходный пост ниже. Также см. документацию здесь и еще один пример здесь.

if __name__ == "__main__":
    from traits.etsconfig.api import ETSConfig
    ETSConfig.toolkit = 'qt4'
    #
    import warnings
    warnings.filterwarnings(action = "ignore", category = FutureWarning, module="chaco")
    warnings.filterwarnings(action = "ignore", category = FutureWarning, module="traits")
#
from PySide import QtGui, QtCore
import numpy as np
from enable.api import Component, Container, Window
from chaco.api import *
import sys
#
class ChacoPlot(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ChacoPlot, self).__init__(parent)
        #
        self.container = OverlayPlotContainer(padding=40)
        #
        self.enableWindow = Window(self, -1, component=self.container)
        windowLayout = QtGui.QVBoxLayout(self)
        windowLayout.addWidget(self.enableWindow.control)
        #
        self.xRange = DataRange1D()
        self.yRange = DataRange1D()
        #
        self.xMapper = LinearMapper(range=self.xRange)
        self.yMapper = LinearMapper(range=self.yRange)
        #
        self.plots = {}
        # keep a list of plots added to the container
    #
    def setMinimumSize(self, width, height):
        self.enableWindow.control.setMinimumSize(width, height)
    #
    def addLine(self, name, plotType):
        xSource = ArrayDataSource([0])
        ySource = ArrayDataSource([0])
        #
        self.xRange.add(xSource)
        self.yRange.add(ySource)
        #
        index_mapper = self.xMapper
        value_mapper = self.yMapper
        #
        # plotType is a class name
        plot = plotType(    index = xSource,
                            value = ySource,
                            index_mapper = index_mapper,
                            value_mapper = value_mapper,
                            visible = False
                       )
        #
        self.container.add(plot)
        #
        self.plots[name] = {'plot':plot, 'xSource':xSource, 'ySource':ySource}
    #
    def updateLine(self, name, xData, yData):
        plot = self.plots[name]
        #
        if np.array(xData).size != 0:
            plot['plot'].visible = True
        else:
            plot['plot'].visible = False
            xData = [0]
            yData = [0]
        #
        plot['xSource'].set_data(xData)
        plot['ySource'].set_data(yData)
    #
    def addAxis(self, plotName, orientation):
        plot = self.plots[plotName]['plot']
        #
        if orientation == 'top' or orientation == 'bottom':
            mapper = self.xMapper
        if orientation == 'left' or orientation == 'right':
            mapper = self.yMapper
        #
        axis = PlotAxis(plot, orientation=orientation, mapper=mapper)
        plot.overlays.append(axis)
        #
        return axis
    #
    def addMinorAxis(self, plotName, orientation):
        plot = self.plots[plotName]['plot']
        #
        if orientation == 'top' or orientation == 'bottom':
            mapper = self.xMapper
            range = self.xRange
        if orientation == 'left' or orientation == 'right':
            mapper = self.yMapper
            range = self.yRange
        #
        newAxis = MinorPlotAxis(plot, orientation=orientation, mapper=mapper)
        plot.overlays.append(newAxis)
        #
        return axis
    #
#
if __name__ == "__main__":
    appQT = QtGui.QApplication.instance()
    #
    x1 = np.arange(300)/18.0
    y1 = np.sin(x1)
    x2 = np.arange(300)/18.0
    y2 = 2.0*np.cos(x2)
    #
    plot = ChacoPlot()
    plot.setMinimumSize(400,300)
    #
    plot.addLine('line1', LinePlot)
    plot.addLine('line2', LinePlot)
    plot.updateLine('line1', x1, y1)
    plot.updateLine('line2', x2, y2)
    #
    plot.addAxis('line1', 'bottom')
    plot.addAxis('line1', 'left')
    plot.addMinorAxis('line1', 'bottom')
    plot.addMinorAxis('line1', 'left')
    #
    plot.show()
    appQT.exec_()

Исходный вариант: в Chaco нет этой функции, но вы можете добавить небольшие отметки, добавив дополнительные PlotAxis. Вам нужно изменить несколько свойств оси:

  • tick_generator — этот объект определяет положение тиков
  • tick_label_formatter — эта функция возвращает строку метки тика для заданного значения метки тика.
  • tick_in и tick_out — эти числа определяют размер делений (внутри и вне оси).

Вот пример. Это много кода, но это довольно просто. Хотя люди обычно строят графики, используя Plot вспомогательный класс, мне нравится создавать графики вручную, так как их намного проще настроить. В любом случае надеюсь, что это поможет.

if __name__ == "__main__":
    from traits.etsconfig.api import ETSConfig
    ETSConfig.toolkit = 'qt4'
    #
    import warnings
    warnings.filterwarnings(action = "ignore", category = FutureWarning, module="chaco")
    warnings.filterwarnings(action = "ignore", category = FutureWarning, module="traits")
#
from PySide import QtGui, QtCore
import numpy as np
from enable.api import Component, Container, Window
from chaco.api import *
import sys
#
class ChacoPlot(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ChacoPlot, self).__init__(parent)
        #
        self.container = OverlayPlotContainer(padding=40)
        #
        self.enableWindow = Window(self, -1, component=self.container)
        windowLayout = QtGui.QVBoxLayout(self)
        windowLayout.addWidget(self.enableWindow.control)
        #
        self.xRange = DataRange1D()
        self.yRange = DataRange1D()
        #
        self.xMapper = LinearMapper(range=self.xRange)
        self.yMapper = LinearMapper(range=self.yRange)
        #
        self.plots = {}
        # keep a list of plots added to the container
    #
    def setMinimumSize(self, width, height):
        self.enableWindow.control.setMinimumSize(width, height)
    #
    def addLine(self, name, plotType):
        xSource = ArrayDataSource([0])
        ySource = ArrayDataSource([0])
        #
        self.xRange.add(xSource)
        self.yRange.add(ySource)
        #
        index_mapper = self.xMapper
        value_mapper = self.yMapper
        #
        # plotType is a class name
        plot = plotType(    index = xSource,
                            value = ySource,
                            index_mapper = index_mapper,
                            value_mapper = value_mapper,
                            visible = False
                       )
        #
        self.container.add(plot)
        #
        self.plots[name] = {'plot':plot, 'xSource':xSource, 'ySource':ySource}
    #
    def updateLine(self, name, xData, yData):
        plot = self.plots[name]
        #
        if np.array(xData).size != 0:
            plot['plot'].visible = True
        else:
            plot['plot'].visible = False
            xData = [0]
            yData = [0]
        #
        plot['xSource'].set_data(xData)
        plot['ySource'].set_data(yData)
    #
    def addAxis(self, plotName, orientation):
        plot = self.plots[plotName]['plot']
        #
        if orientation == 'top' or orientation == 'bottom':
            mapper = self.xMapper
        if orientation == 'left' or orientation == 'right':
            mapper = self.yMapper
        #
        axis = PlotAxis(plot, orientation=orientation, mapper=mapper)
        plot.overlays.append(axis)
        #
        return axis
    #
    def addMinorAxis(self, plotName, orientation):
        plot = self.plots[plotName]['plot']
        #
        if orientation == 'top' or orientation == 'bottom':
            mapper = self.xMapper
            range = self.xRange
        if orientation == 'left' or orientation == 'right':
            mapper = self.yMapper
            range = self.yRange
        #
        newAxis = PlotAxis(plot, orientation=orientation, mapper=mapper)
        plot.overlays.append(newAxis)
        #
        newAxis.tick_generator = MinorTickGenerator()
        #
        newAxis.tick_label_formatter  = lambda x: ''
        newAxis.tick_in  = 2
        newAxis.tick_out = 2
    #
#
class MinorTickGenerator(AbstractTickGenerator):
    def __init__(self):
        super(MinorTickGenerator, self).__init__()
    #
    def get_ticks(self, data_low, data_high, bounds_low, bounds_high, interval, use_endpoints=False, scale='linear'):
        interval = interval
        #
        if interval == 'auto':
            interval = auto_interval(data_low, data_high)/5.0
        #
        return auto_ticks(data_low, data_high, bounds_low, bounds_high, interval, use_endpoints)
    #
#
if __name__ == "__main__":
    appQT = QtGui.QApplication.instance()
    #
    x1 = np.arange(300)/18.0
    y1 = np.sin(x1)
    x2 = np.arange(300)/18.0
    y2 = 2.0*np.cos(x2)
    #
    plot = ChacoPlot()
    plot.setMinimumSize(400,300)
    #
    plot.addLine('line1', LinePlot)
    plot.addLine('line2', LinePlot)
    plot.updateLine('line1', x1, y1)
    plot.updateLine('line2', x2, y2)
    #
    plot.addAxis('line1', 'bottom')
    plot.addAxis('line1', 'left')
    plot.addMinorAxis('line1', 'bottom')
    plot.addMinorAxis('line1', 'left')
    #
    plot.show()
    appQT.exec_()

введите здесь описание изображения

person Steve    schedule 17.01.2016