PDA

View Full Version : Zoom and scroll (pyqt5 -python)



quant
27th August 2019, 15:24
In "matplotlib" to install the viewing range of the x-axis used the function "set_xlim"
That is, after installing "step" by means of "Slider" I used the following code:

def update(evt=None):
r = scroll.value() / ((1 + step) * 100)
l1 = lims[0] + r * np.diff(lims)#lims - is the first and last value on the x axis(two values)
l2 = l1 + np.diff(lims) * step
ax.set_xlim(l1, l2)
So I was able to set the scale and scroll the chart.
Is there any Qt function is similar to "set_xlim"?

d_stranz
28th August 2019, 17:04
Is there any Qt function is similar to "set_xlim"?

Your question doesn't make much sense. matplotlib is a python library for scientific graphics and has nothing to do with Qt, which is a set of C++ libraries for application development. PyQt is one of several python wrappers around parts of the Qt libraries that you can use to implement Qt GUIs in python..

Your code looks like it is updating the value of a plot axis range based on the position of a slider. "ax" is the variable representing the axis, and "set_xlim" is the method of the axis class that sets the axis range.

What kind of Qt class could you be looking for that would have a similar function? And why are you looking for it?

quant
28th August 2019, 22:21
Your question doesn't make much sense. matplotlib is a python library for scientific graphics and has nothing to do with Qt, which is a set of C++ libraries for application development. PyQt is one of several python wrappers around parts of the Qt libraries that you can use to implement Qt GUIs in python..

Your code looks like it is updating the value of a plot axis range based on the position of a slider. "ax" is the variable representing the axis, and "set_xlim" is the method of the axis class that sets the axis range.

What kind of Qt class could you be looking for that would have a similar function? And why are you looking for it?


QtChart. Found setRange(). Question closed.
Yes I use the scroll and zoom horizontally.

Now I have another task. The thing is, I use a candlestick chart.
In pyqt5, the graph is slightly faster than in matplotlib.
But it's still slow. What I can not say about the line graph, where drawing is much faster.
In order not to be verbose, I outlined the essence of stackoverflow https://stackoverflow.com/questions/57699422/are-fast-candlestick-charts-possible-in-python

If not welcome reference on other resources, then request to the moderator to remove exile.

d_stranz
29th August 2019, 02:26
QtChart. Found setRange(). Question closed.

Then you weren't asking about a specific matplotlib function at all. You were asking about a complete Qt-based replacement for a plot that you can produce in matplotlib.


But it's still slow.

The problem (from your stackoverflow post) is that you are updating the axis for every single move of the scrollbar. QtChart will redraw itself every time the axis range is updated. If you have 40,000 candles, every candle will be redrawn on every step. Of course it will be slow.

One solution is to not update the axis for every step, but only update the plot when the slider finally stops moving. Or update it every 10 steps and again when it stops moving.

To suspend updates while the slider is moving, implement a QTimer with a timeout of 200 ms or something like that. Each time the slider moves, you record the current position in a member variable for your class and restart the timer for another 200 ms. When the user stops moving the slider, the timer will timeout. In the timeout slot, you finally update the axis with the final slider value. By repeatedly restarting the time, you ensure that nothing gets drawn until 200 ms after the slider finally stops moving.

If you want to periodically update the plot even while the slider is still moving, you can do the same thing with the QTimer, but also add a member variable that counts the number of steps. Whenever steps == 10 (or whatever), update the axis and set steps = 0, otherwise just set steps = steps + 1 and do nothing. The QTimer will ensure that when the slider stops moving, the plot will be updated with the final value no matter how many steps there were.

The timer + step counting method will give something that looks almost like a real time update, but will give you much better performance. You can set the numbers of steps or timeout so that the scrolling looks smooth because the redraw can keep up with the slider.

quant
29th August 2019, 12:34
The problem (from your stackoverflow post) is that you are updating the axis for every single move of the scrollbar. QtChart will redraw itself every time the axis range is updated. If you have 40,000 candles, every candle will be redrawn on every step. Of course it will be slow.


Thanks, I'll try!
In Python, there are only about 7000 candles, it is in the graph on C# 40,000 candles.
C# uses the "chart control" library, which is not considered fast.

d_stranz
29th August 2019, 15:34
Thanks, I'll try!

Another thing you can try: If the line plot is fast, then think about drawing only a line plot during zooming or scrolling. That is, when scrolling starts, replace the candle plot series with a line series and update that. When scrolling stops, replace the line series with the candle series again. You can use the QTimer trick to know when scrolling has stopped. You will give the impression of real time scrolling, but you will have only a small fraction of the things to draw.

I don't think users will care if the candles disappear during scrolling. Many graphics software apps reduce the level of detail during interactive procedures to get better performance. As soon as scrolling stops, the full plot will reappear.