Skip to content

Feature: Independent horizontal/vertical line width (elliptical/anisotropic pen, asymmetrical shape) #12980

Closed as not planned

Description

@nyanpasu64

Bug report

Bug summary

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.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.random.rand(2000))
plt.show()

Actual outcome

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).

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 3.0.0 Anaconda main channel
  • Matplotlib backend (print(matplotlib.get_backend())): module://backend_interagg
  • Python version: 3.6.6

Ideas

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?

Activity

  1. nyanpasu64 commented on Dec 16, 2018

    @nyanpasu64
    Author

    Are there any updates on this feature request?

  2. dstansby commented on Dec 16, 2018

    @dstansby
    Member

    Does doing plt.plot(..., linewidth=0.5) solve the issue?

  3. nyanpasu64 commented on Dec 16, 2018

    @nyanpasu64
    Author

    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).

  4. jklymak commented on Dec 16, 2018

    @jklymak
    Member

    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.

  5. nyanpasu64 commented on Dec 16, 2018

    @nyanpasu64
    Author

    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).

  6. jklymak commented on Dec 16, 2018

    @jklymak
    Member

    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.

  7. nyanpasu64 commented on Dec 16, 2018

    @nyanpasu64
    Author

    As I stated in my original post...

    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:

    • Stretch path horizontally
    • Then cast to stroke_t stroke (to find outline of path)
    • Unstretch stroke afterwards.

    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.

  8. anntzer commented on Dec 16, 2018

    @anntzer
    Contributor

    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.

  9. nyanpasu64 commented on Dec 17, 2018

    @nyanpasu64
    Author

    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

    What would the API (both on the artist, and on the renderer) look like?

    Line (is that what you mean by artist?)

    • ax.plot(data, color=line_color, linewidth=line_width, xsqueeze=3)
    • 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.
    • Naming issue: the use of "squeeze" may conflict with plt.subplots(squeeze=) (https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html).

    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.

  10. anntzer commented on Dec 17, 2018

    @anntzer
    Contributor

    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).
  11. nyanpasu64 commented on Dec 17, 2018

    @nyanpasu64
    Author

    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?

  12. anntzer commented on Dec 17, 2018

    @anntzer
    Contributor

    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).

  13. jklymak commented on Dec 17, 2018

    @jklymak
    Member

    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.

  14. tacaswell commented on Dec 18, 2018

    @tacaswell
    Member

    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 Line2D to implement this.

    That said, we do have some Agg only features, such as agg_filter that @anntzer mentioned above.

    import numpy as np
    import matplotlib.pyplot as plt
    from skimage.transform import resize, rescale
    
    
    def agg_filter(arr, dpi):
        out = stretch_and_resample(arr)
        return out, 0, 0
    
    fig, ax = plt.subplots()
    th = np.linspace(-20, 20, 1024)
    ln, = ax.plot(th, np.sin(th*10) / th, lw=5)
    ln.set_agg_filter(agg_filter)

    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!

  15. nyanpasu64 commented on Dec 18, 2018

    @nyanpasu64
    Author

    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?

  16. github-actions commented on May 25, 2023

    @github-actions

    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!

  17. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    New featurestatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions