Hi,
just a small change in your code does the trick:
...
graph1Text = pg.TextItem(text = 'A1', color=(0, 0, 0))
# graph1.addItem(graph1Text)
graph1_vb = graph1.getViewBox()
graph1Text.setParentItem(graph1_vb)
graph1Text.setPos(150, 1)
...
You can look it up in PlotItem.py in the function addLegend(...).
But if it's just something like a headline or describing text I would
recomment to use the GraphicsLayout to put a labelItem above or below your
plot.
Cheers,
Sebastian
Am Montag, 26. Juni 2017 01:17:42 UTC+2 schrieb wot:
>
> import pyqtgraph as pg
> from PyQt4 import QtGui
> import numpy as np
> import sys
> def main():
> app = QtGui.QApplication(sys.argv)
> widg = QtGui.QWidget()
> widg.move(100, 100)
> pg.setConfigOption('background', 'w')
> pg.setConfigOption('foreground', 'k')
>
> pgWidg = pg.GraphicsLayoutWidget()
> pgWidg.resize(750, 250)
>
> graph1 = pgWidg.addPlot(row=1, col=1)
> graph2 = pgWidg.addPlot(row=1, col=2)
> curve1 = graph1.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
> curve2 = graph2.plot(y=np.sin(np.linspace(1, 21, 1000)), pen='k')
>
> graph1.addItem(curve1)
> graph2.addItem(curve2)
> graph1.setMouseEnabled(x=False, y=True)
> graph2.setMouseEnabled(x=False, y=True)
>
> graph1Text = pg.TextItem(text = 'A1', color=(0, 0, 0))
> graph1.addItem(graph1Text)
> graph1Text.setPos(150, 1)
>
> legend = graph2.addLegend()
> style = pg.PlotDataItem(pen='w')
> legend.addItem(style, 'A2')
>
> grid = QtGui.QGridLayout()
> grid.addWidget(pgWidg, 0,0)
> widg.setLayout(grid)
> widg.show()
> sys.exit(app.exec_())
>
> if __name__ == '__main__':
>
> main()
>
>
> This example code illustrates the problem. On the lefthand graph, scaling
> the y-axis causes the text to move whereas on the righthand graph the
> legend stays in a constant position as you scale. I would like the position
> of the textItem to be defined like the legend position, in a constant
> position relative to the graph window. Alternatively if someone knows how
> to change the format of the legend and update the text that would also
> work, but from my reading of the documentation this is not possible.
>
> On Saturday, June 24, 2017 at 6:40:50 PM UTC-4, wot wrote:
>>
>> I would like a TextItem that maintains a constant position on the graph
>> while scaling, essentially the same functionality as legend only a TextItem.
>> Cannot figure out how to do this. Any suggestions welcome.
>>
>>
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/7b84eb49-9c9e-4d07-9aaf-273bd2807e17%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.