How to add custom AxisItem to existing PlotWidget?

pyqtgraph axisitem
pyqtgraph plotitem

I'm trying to add custom AxisItem in pyqtgraph to existing PlotWidget that was generated by Qt Designer. There is related topic here, but there is no exact answer with code example and I cannot comment, so I've created a new topic.

This is my custom AxisItem (based on this code):

import pyqtgraph as pg
import datetime

def int2td(ts):
    return(datetime.timedelta(seconds=float(ts)/1e6))

class TimeAxisItem(pg.AxisItem):
    def __init__(self, *args, **kwargs):
        super(TimeAxisItem, self).__init__(*args, **kwargs)
    def tickStrings(self, values, scale, spacing):
        return [int2dt(value).strftime("%H:%M:%S") for value in values]

This is my main QtPlotter class:

from pyqtgraph.Qt import QtGui
from template_pyqt import Ui_Form # Ui_Form is generated by Qt Designer

class QtPlotter:
    def __init__(self):
        self.app = QtGui.QApplication([])
        self.win = QtGui.QWidget()
        self.ui = Ui_Form()
        self.ui.setupUi(self.win)
        self.win.show()

        self.ui_plot = self.ui.plot 
        self.ui_plot.showGrid(x=True, y=True)

And then I'm trying to add my custom AxisItem:

self.ui_plot.getPlotItem().axes['bottom']['item'] = TimeAxisItem(orientation='bottom')

I have no errors, but this does not give any effect.

The PlotItem class does not have a setAxis method, only a getAxis method to get the current axis. You can specify a dictionary with axis items in the axisItems parameter of the PlotItem constructor, but it doesn't seem possible to update an AxisItem of an existing PlotItem.

Even though the PlotItem.axes dictionary is a public attribute, it is undocumented and just using it is therefore a bit risky. There is a possibility that the author of PyQtGraph will alter its behavior, or rename it (although the change of this happening is small). IMHO it should have been made a private attribute.

In any case, by looking at the source of the PlotItem constructor you can see how the axis items from axisItem parameter are added to the plot item:

    ## Create and place axis items
    if axisItems is None:
        axisItems = {}
    self.axes = {}
    for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))):
        if k in axisItems:
            axis = axisItems[k]
        else:
            axis = AxisItem(orientation=k, parent=self)
        axis.linkToView(self.vb)
        self.axes[k] = {'item': axis, 'pos': pos}
        self.layout.addItem(axis, *pos)
        axis.setZValue(-1000)
        axis.setFlag(axis.ItemNegativeZStacksBehindParent)

Perhaps you can make it work by looking at the code above. Again, this is undocumented behavior, use at your own risk! Furthermore, you should properly remove and unlink the old AxisItem when setting a new one to prevent memory leaks. Perhaps this is tricky and this could be the reason why the PlotItem.setAxis method does not exist.

python - How to add custom AxisItem to existing PlotWidget?, The PlotItem class does not have a setAxis method, only a getAxis method to get the current axis. You can specify a dictionary with axis items in the axisItems  I'm trying to add custom AxisItem in pyqtgraph to existing PlotWidget that was generated by Qt Designer. There is related topic here, but there is no exact answer with code example and I cannot comment there, so I've created a new topic again.

I looked into the source of the PlotItem constructor for a complete day, but I couldn't get it to work.

In the end, I found that QtDesigner/QtCreator only outputs three lines on the PlotWidget. Instead of trying to add a TimeAxisItem to the existing PlotWidget, it is easier (but definitely not nicer) to just delete the existing PlotWidget and then make a new one with a TimeAxisItem, as described here.

from PyQt5 import QtCore
import pyqtgraph as pg

parent = self.ui_plot.parent()
geom_object = self.ui_plot.frameGeometry()
geometry = QtCore.QRect(geom_object.left(), geom_object.top(), geom_object.width(), geom_object.height())
object_name = self.ui_plot.objectName()

del self.ui_plot

time_axis = TimeAxisItem(orientation='bottom')

self.ui_plot = pg.PlotWidget(parent, axisItems={'bottom': time_axis})
self.ui_plot.setGeometry(geometry)
self.ui_plot.setObjectName(object_name)

How to add custom AxisItem to existing PlotWidget?, I'm trying to add custom AxisItem in pyqtgraph to existing PlotWidget that was generated by Qt Designer. There is related topic here, but there is  The following are code examples for showing how to use pyqtgraph.AxisItem(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Since there are people that try to find something here, I will post my simple but not elegant workaround.

After converting my template generated by Qt Designer to python template file with pyuic, I'm adding my custom AxisItem directly to python template file by replacing corresponding PlotWidget. This all can be done with simple bash script:

pyuic4 template.ui -o template.py 

match="from PyQt4 import QtCore, QtGui"
insert_class="from timeaxisitem_class import TimeAxisItem"
match_widget="self.plot = PlotWidget(Form)"
insert_timeaxis="self.plot = PlotWidget(Form, axisItems={'bottom': TimeAxisItem(orientation='bottom')})"

file="template.py"
sed -i "s/$match/$match\n$insert_class/" $file
sed -i "s/$match_widget/$insert_timeaxis/" $file

Plotting in pyqtgraph, PlotItem.plot(), Add a new set of data to an existing plot widget PlotItem - Contains a ViewBox for displaying data as well as AxisItems and labels for displaying  QCustomPlot is a Qt C++ widget for plotting and data visualization. It has no further dependencies and is well documented.This plotting library focuses on making good looking, publication quality 2D plots, graphs and charts, as well as offering high performance for realtime visualization applications.

I know this is an old post but I am encountering a similar issue and came across a solution in this gist example, which is an AxisItem subclass that implements an attachToPlotItem method.

So regarding the original post, instead of

self.ui_plot.getPlotItem().axes['bottom']['item'] = TimeAxisItem(orientation='bottom')

you need the following method (copied from the example linked above) in your TimeAxisItem subclass:

    def attachToPlotItem(self, plotItem):
        """Add this axis to the given PlotItem
        :param plotItem: (PlotItem)
        """
        self.setParentItem(plotItem)
        viewBox = plotItem.getViewBox()
        self.linkToView(viewBox)
        self._oldAxis = plotItem.axes[self.orientation]['item']
        self._oldAxis.hide()
        plotItem.axes[self.orientation]['item'] = self
        pos = plotItem.axes[self.orientation]['pos']
        plotItem.layout.addItem(self, *pos)
        self.setZValue(-1000)

It even retains the original AxisItem so that it could be re-instated if needed.

Then in your QtPlotter constructor, you'd add

axis = TimeAxisItem(orientation='bottom')
axis.attactToPlotItem(self.ui_plot.getPlotItem())

AxisItem, AxisItem (orientation, pen=None, linkView=None, parent=None, maxTickLength​=-5, (This is usually accessed by changing the log mode of a PlotItem ). setPen​  QCustomPlot is a Qt C++ widget for plotting. This plotting library focuses on making good looking, publication quality 2D plots, graphs and charts, as well as offering high performance for realtime visualization.

Embedding PyQtGraph (or any other custom PyQt5 widgets) from Qt , If the custom widget you are adding is a subclass of an existing Qt widget, you may from PyQt5 import QtWidgets, uic from pyqtgraph import PlotWidget, plot  After you create a chart, you can add axis titles to the horizontal and vertical axes in charts that have axes. You can’t add them to charts that don’t have axes such as pie and doughnut charts. Much like a chart title you can add, axis titles help the people who view the chart understand what the data is about.. Here’s how you add axis

pyqtgraph.PlotWidget Python Example, The following are code examples for showing how to use pyqtgraph. PlotWidget(*args, **kwds) data = pg.np.array([1,5,2,4,3]) c = w.plot(data, name='​stuff') w. Plotting in pyqtgraph¶. There are a few basic ways to plot data in pyqtgraph: All of these will accept the same basic arguments which control how the plot data is interpreted and displayed: x - Optional X data; if not specified, then a range of integers will be generated automatically. y - Y data.

Hanging xaxis whe trying to supply custom AxisItem to PlotWidget in , Hanging xaxis whe trying to supply custom AxisItem to PlotWidget in PyQt4. (too old to reply) use a custom xaxis (with time formatting, but that is besides the point). existing xaxis and monkey patching tickStrings, but I would rather not have If the custom widget you are adding is a subclass of an existing Qt widget, you may want to use the base class as your placeholder to promote from. For example, if you have a custom MyAwesomeButton button widget subclassed from QPushButton use QPushButton as the placeholder and promote it to MyAwesomeButton .

Comments
  • use deleteLater() work for me better! see: stackoverflow.com/questions/4528347/…