You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature: Independent horizontal/vertical line width (elliptical/anisotropic pen, asymmetrical shape) #12980
I would like the ability to render plots where lines are asymmetrical, such that vertical lines have a narrow width, while horizontal lines have a larger height.
I lose most horizontal detail, and the overlapping lines essentially fill the entire screen with blue. This is because horizontal points are uniformly spaced, but vertical points are random.
Expected outcome
The ability to make vertical lines much thinner, so I can see fine detail more clearly, while low-frequency sections produce tall horizontal lines.
I'm encountering this issue in my audio oscilloscope program, which smears out horizontal detail when plotting complex waves. I usually plot 500-2000 samples in 600-1200 pixels, but decreasing the line width makes the line hard to see (especially on mobile and Youtube thumbnails).
agg math_stroke assumes a fixed aspect ratio, and is used to calculate corners (and probably ends) which controls stroke shape.
The easiest solution is to edit _backend_agg.h and change RendererAgg::_draw_path to stretch path horizontally before casting to stroke_t stroke, and unstretch stroke afterwards. I can't implement this using a pure Python subclass, and don't want to build my own matplotlib C++ to bundle with my app.
Complaints
agg's duck-typed templates are baffling to understand. Maybe I'm just not well-versed in Qt Creator. How do I show the computed template-type of various objects, and the types accepted?
Decreasing line width is not that I want. It causes less complex (near-horizontal) line sections to be very hard to see.
I could tell users to decrease line width for more complex channels. The issue is, complexity changes over time, the same wave can have complex and simple portions, and I think adjacent channels having different line widths may be ugly.
Another oscilloscope program (Ovgen, I believe it uses .net graphics) allows switching between a slightly narrow pen, and a near-zero-width pen. Example at https://youtu.be/ay5K7-s7XQU (you could argue that the narrow vertical pen is hard to see, but i think aspect ratio should be configurable).
I’m 99.9% sure that example is not switching widths. The horizontal portions have noise around a constant value and hence the line has thickness, wherase the periodic jumps appear thinner.
I'm not sure if that video uses this feature or not, but I'm certain this feature exists. EDIT: I just rewatched the video at native 720p, and I'm sure this uses "CRT mode" since vertical lines are exactly 1 pixel wide (not antialiased).
As a side note, these recordings are made digitally through emulators which produce zero noise (aside from quantization/dithering and internal program quantization, and Gibbs-effect ringing (not visible in this video) which causes increased height near vertical lines).
I’m skeptical this is of general enough interest to find its way into the main library. If you’d like this feature you could make a standalone package and if you build a good sized community we could consider adding.
From a practical point of view I don’t see how you differentiate between an upstroke and a horizontal stroke. What do you do for a 45 degree stroke? In Matplotlib we don’t differentiate.
I think the feature request is worth considering (as in, we may consider a PR implementing it, because I don't know any of the core devs who really looks forward to playing with the Agg code...), but needs to consider the following points:
What would the API (both on the artist, and on the renderer) look like?
This should hopefully work for both Line2D and LineCollection.
This should work not only for the Agg backend, but also for the vector ones (pdf/ps/svg), and at least not completely break third-party renderers (even if they don't support the feature).
Moreover, I think it may be possible to implement this on Python's side by (ab)using the agg_filter mechanism.
It would also be nice to figure out what knobs we can expose to make this doable from a third-party package.
I only need xsqueeze but maybe add ysqueeze for symmetry. I think there is no valid reason to supply both parameters at once, you should adjust linewidth instead.
Multiply the chosen dimension by the factor before outlining, then divide coordinates afterwards.
The call stack is long and convoluted (most recent call first):
Line2D.__init__
_process_plot_var_args._makeline (why does a class have a snake_case name?)
_process_plot_var_args._plot_args
_process_plot_var_args._grab_next_args
Axes.plot
Probably add xsqueeze/ysqueeze kwargs into Line2D.__init__()? (I'm a fan of using attr.dataclass(kw_only=True) to group kwargs).
store in gc bad idea, gc is a disposable local object created in each draw() call.
Renderer
Is there a reason to add this feature to renderers other than Agg, and is that a prerequisite for merging a PR? Cairo was much slower than Agg for me, and I don't need this feature for SVG export/etc.
Line2D.draw() calls:
gc is created and disposed in each draw() call, and is a renderer.new_gc() (GraphicsContext?)
gc.set_linewidth(self._linewidth)
renderer.draw_path(gc, tpath, affine.frozen())
python RendererAgg.draw_path is a thin wrapper (does something related to agg.path.chunksize) around c++ RendererAgg::draw_path.
Renderer proposal
Performance is very important for me, as I'm rendering 60 fps at 1280x720 with 1-10k points. It would be a shame if I ended up not using this feature because of the speed hit... :(
I would use an if(xsqueeze != 1) to skip the multiplication speed hit, if no squeezing occurred.
I don't know how to avoid overhead when distortion is disabled. Unconditionally multiplying coordinates will add overhead if multiplying by 1. To avoid that, I feel a runtime branch must happen in either Python or C++, and if/else in C++ will be faster (unless when plotting LineCollection, Python performs 1 branch but C++ performs O(nlines) branches).
a. Add an if-else into Python.
a. Add an if-else into draw_path to call variants of _draw_path (_draw_path_xsqueeze and _draw_path_ysqueeze).
- draw_path looks at gc, _draw_path either arguments or gc.
a. Add an if-else into _draw_path
- Pass squeeze factors via gc.
- I don't know if the if/else statement's resulting stroke variables are type-compatible.
Optimization: You transform path points from input space to graph space. Instead of transforming path points twice, you could multiply the "screen width" by xsqueeze.
Thank you for the detailed proposal.
It's unlikely I will have the time to look at this carefully before new year (end of year squeeze...) but others should feel free to continue the discussion in the meantime.
Quick random notes:
Instead of x/ysqueeze, I'd suggest something like pen_transform (name up to bikeshedding) which would be an Affine2D transform whose inverse is applied pre-render and which is applied post-render, this way you also get "slanted" pens for free for the same amount of code. I'd add a GraphicsContextBase.set_pen_transform method and let the renderers look at that.
I don't care much about the builtin cairo renderer (which is basically beyond repair IMO; see also https://github.com/anntzer/mplcairo in case you're interested in cairo), but someone will inevitably ask why this doesn't work for pdf/ps/svg if you only implement it for Agg, so at least some idea of feasibility would be nice (and pdf/ps/svg export should at least not crash and preferably do something sensible like ignore the setting -- which should be the case if going the GraphicsContextBase.set_pen_transform route).
What's wrong with the Cairo backend? (for performance, I disable all artists other than background and lines. I only noticed the slow speed.) Would the new backend be faster than agg?
The cairo backend does renders math not so well (you can try for example plt.text(.5, .5, r"$\sqrt{2}$")) and this is unlikely to be ever fixed because pycairo only exposes cairo's "toy" font API.
The poor performance of the cairo backend is basically unfixable using pure Python code (I really tried...).
It also does not implement hatching, but this could in theory be fixed.
My implementation of the cairo backend is competitive with Agg (it is faster for some plots, slower for other -- there's some benchmarking code in the repo, and this is also detailed in the README).
I’m still against this as a general feature in the plot codepath. I don’t think simulating an analog plotting artifact is of general enough interest to merit a new kwarg and added code complexity.
I agree with most of what @anntzer said. If we are going to implement pen shapes they should be supported across all of the 'core' backends where it can be supported (we try to keep the backends as close to parity as possible) or a very compelling case that this is impossible in the vector backends and across everything that get stroked (Line2D, LineCollections, Polygons, ...). This is obviously a pretty big amount of work for something I do not see a clear path to.
I am very 👎 on a Agg specific path in Line2D to implement this.
That said, we do have some Agg only features, such as agg_filter that @anntzer mentioned above.
but I have been failing at getting the resampling from skimage to work the way I want it to...
If you are concerned about performance you may be better served by https://github.com/pyqtgraph/pyqtgraph which either user the Qt native 2D artists or openGL. That said, I am happy to know you are getting 60fps out of Matplotlib!
Pyqtgraph's image-bytes extractor (I popped up to ffmpeg) is horrifically slow. After rewriting it, I managed to get 20% more fps than matplotlib agg (axes disabled for both plots). However, on pyqtgraph, setting line width > 1 (which enables outline-then-fill) caused performance to drop below matplotlib.
Enabling gpu/opengl on pyqtgraph seemed to have no effect on performance. (Maybe I could investigate further.)
Should I avoid discussing pyqtgraph on a matplotlib issue tracker?
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
nyanpasu64 commentedon Dec 16, 2018
Are there any updates on this feature request?
dstansby commentedon Dec 16, 2018
Does doing
plt.plot(..., linewidth=0.5)solve the issue?nyanpasu64 commentedon Dec 16, 2018
Decreasing line width is not that I want. It causes less complex (near-horizontal) line sections to be very hard to see.
I could tell users to decrease line width for more complex channels. The issue is, complexity changes over time, the same wave can have complex and simple portions, and I think adjacent channels having different line widths may be ugly.
Another oscilloscope program (Ovgen, I believe it uses .net graphics) allows switching between a slightly narrow pen, and a near-zero-width pen. Example at https://youtu.be/ay5K7-s7XQU (you could argue that the narrow vertical pen is hard to see, but i think aspect ratio should be configurable).
jklymak commentedon Dec 16, 2018
I’m 99.9% sure that example is not switching widths. The horizontal portions have noise around a constant value and hence the line has thickness, wherase the periodic jumps appear thinner.
nyanpasu64 commentedon Dec 16, 2018
ovgen has a feature to use a narrow pen: https://github.com/Zeinok/OVGen/blob/49fec909c7d86628e7e3e212aa68df8251c4198f/OVGen/MainForm.vb#L225-L230
wavePen is a
System.Drawing.Pen.I'm not sure if that video uses this feature or not, but I'm certain this feature exists. EDIT: I just rewatched the video at native 720p, and I'm sure this uses "CRT mode" since vertical lines are exactly 1 pixel wide (not antialiased).
As a side note, these recordings are made digitally through emulators which produce zero noise (aside from quantization/dithering and internal program quantization, and Gibbs-effect ringing (not visible in this video) which causes increased height near vertical lines).
jklymak commentedon Dec 16, 2018
I’m skeptical this is of general enough interest to find its way into the main library. If you’d like this feature you could make a standalone package and if you build a good sized community we could consider adding.
From a practical point of view I don’t see how you differentiate between an upstroke and a horizontal stroke. What do you do for a 45 degree stroke? In Matplotlib we don’t differentiate.
nyanpasu64 commentedon Dec 16, 2018
As I stated in my original post...
agg
math_strokeassumes a fixed aspect ratio, and is used to calculate corners (and probably ends) which controls stroke shape.The easiest solution is to edit
_backend_agg.hand changeRendererAgg::_draw_path:pathhorizontallystroke_t stroke(to find outline ofpath)strokeafterwards.This will make vertical lines thinner by
stretching factor, leave horizontal lines untouched, and affect angled lines by a different amount.I can't implement this using a pure Python subclass, and don't want to build my own matplotlib C++ to bundle with my app.
anntzer commentedon Dec 16, 2018
I think the feature request is worth considering (as in, we may consider a PR implementing it, because I don't know any of the core devs who really looks forward to playing with the Agg code...), but needs to consider the following points:
Moreover, I think it may be possible to implement this on Python's side by (ab)using the agg_filter mechanism.
It would also be nice to figure out what knobs we can expose to make this doable from a third-party package.
nyanpasu64 commentedon Dec 17, 2018
Are you expecting me to write a proof of concept?
Note: Ovgen calls it "crt mode" because analog oscilloscopes get (dimmer? thinner?) on steep near/vertical lines.
API
Line (is that what you mean by artist?)
ax.plot(data, color=line_color, linewidth=line_width, xsqueeze=3)xsqueezebut maybe addysqueezefor symmetry. I think there is no valid reason to supply both parameters at once, you should adjustlinewidthinstead.plt.subplots(squeeze=)(https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html).The call stack is long and convoluted (most recent call first):
Probably add
xsqueeze/ysqueezekwargs intoLine2D.__init__()? (I'm a fan of usingattr.dataclass(kw_only=True)to group kwargs).store inbad idea,gcgcis a disposable local object created in eachdraw()call.Renderer
Is there a reason to add this feature to renderers other than Agg, and is that a prerequisite for merging a PR? Cairo was much slower than Agg for me, and I don't need this feature for SVG export/etc.
Line2D.draw()calls:gcis created and disposed in eachdraw()call, and is arenderer.new_gc()(GraphicsContext?)gc.set_linewidth(self._linewidth)renderer.draw_path(gc, tpath, affine.frozen())RendererAgg.draw_pathis a thin wrapper (does something related toagg.path.chunksize) around c++RendererAgg::draw_path.Renderer proposal
Performance is very important for me, as I'm rendering 60 fps at 1280x720 with 1-10k points. It would be a shame if I ended up not using this feature because of the speed hit... :(
I would use an
if(xsqueeze != 1)to skip the multiplication speed hit, if no squeezing occurred.I don't know how to avoid overhead when distortion is disabled. Unconditionally multiplying coordinates will add overhead if multiplying by 1. To avoid that, I feel a runtime branch must happen in either Python or C++, and
if/elsein C++ will be faster (unless when plottingLineCollection, Python performs 1 branch but C++ performsO(nlines)branches).a. Add an if-else into Python.
a. Add an if-else into
draw_pathto call variants of_draw_path(_draw_path_xsqueezeand_draw_path_ysqueeze).-
draw_pathlooks atgc,_draw_patheither arguments orgc.a. Add an if-else into
_draw_path- Pass squeeze factors via
gc.- I don't know if the if/else statement's resulting
strokevariables are type-compatible.Optimization: You transform path points from input space to graph space. Instead of transforming path points twice, you could multiply the "screen width" by
xsqueeze.anntzer commentedon Dec 17, 2018
Thank you for the detailed proposal.
It's unlikely I will have the time to look at this carefully before new year (end of year squeeze...) but others should feel free to continue the discussion in the meantime.
Quick random notes:
GraphicsContextBase.set_pen_transformmethod and let the renderers look at that.GraphicsContextBase.set_pen_transformroute).nyanpasu64 commentedon Dec 17, 2018
What's wrong with the Cairo backend? (for performance, I disable all artists other than background and lines. I only noticed the slow speed.) Would the new backend be faster than agg?
anntzer commentedon Dec 17, 2018
The cairo backend does renders math not so well (you can try for example
plt.text(.5, .5, r"$\sqrt{2}$")) and this is unlikely to be ever fixed because pycairo only exposes cairo's "toy" font API.The poor performance of the cairo backend is basically unfixable using pure Python code (I really tried...).
It also does not implement hatching, but this could in theory be fixed.
My implementation of the cairo backend is competitive with Agg (it is faster for some plots, slower for other -- there's some benchmarking code in the repo, and this is also detailed in the README).
jklymak commentedon Dec 17, 2018
I’m still against this as a general feature in the plot codepath. I don’t think simulating an analog plotting artifact is of general enough interest to merit a new kwarg and added code complexity.
tacaswell commentedon Dec 18, 2018
This feature is comparable to the inkscape calligraphy pen (https://inkscape.org/doc/tutorials/calligraphy/tutorial-calligraphy.html with the angle set to 90 and the fixation to 100)?
I agree with most of what @anntzer said. If we are going to implement pen shapes they should be supported across all of the 'core' backends where it can be supported (we try to keep the backends as close to parity as possible) or a very compelling case that this is impossible in the vector backends and across everything that get stroked (
Line2D,LineCollections,Polygons, ...). This is obviously a pretty big amount of work for something I do not see a clear path to.I am very 👎 on a Agg specific path in
Line2Dto implement this.That said, we do have some Agg only features, such as
agg_filterthat @anntzer mentioned above.but I have been failing at getting the resampling from skimage to work the way I want it to...
If you are concerned about performance you may be better served by https://github.com/pyqtgraph/pyqtgraph which either user the Qt native 2D artists or openGL. That said, I am happy to know you are getting 60fps out of Matplotlib!
nyanpasu64 commentedon Dec 18, 2018
Pyqtgraph's image-bytes extractor (I popped up to ffmpeg) is horrifically slow. After rewriting it, I managed to get 20% more fps than matplotlib agg (axes disabled for both plots). However, on pyqtgraph, setting line width > 1 (which enables outline-then-fill) caused performance to drop below matplotlib.
Enabling gpu/opengl on pyqtgraph seemed to have no effect on performance. (Maybe I could investigate further.)
Should I avoid discussing pyqtgraph on a matplotlib issue tracker?
github-actions commentedon May 25, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!