Repository navigation
Description
Problem
Markers can't be rotated in a given angle, this would be useful even for a highschool level (I work making diagrams for a highschool level), and in general to make Matplotlib even better for making geometric diagrams.
Here is an example:
I did this diagram with Matplotlib, the code became cumbersome when trying to make the little angle markers, the ones that are two small lines perpendicular to the angle arcs. So I searched in the documentation and it seems there is no way to make marks like this builtin, there is no way to create a small perpendicular line to the arc in the middle of the arc, but then I thought about using plot markers for this.
The only problem of using markers, is that they can't be rotated, there is no way you can tell Matplotlib to make the marker perpendicular nor tangent to the line in which it's placed, or is there a way? I ask you kindly in case any of you know a way to plot markers perpendicular to the line in which they are placed.
Strangely this feature was being worked on since 2013, but it seems nothing came of it, see #2478
The way I have found to rotate markers, is using a private attribute from the markers called '_transform', this attribute has methods to rotate the marker, but it's private, this means that the API has not provided a way to access and change this attribute. Here is a minimal example to rotate the markers.
import matplotlib.pyplot as plt
from matplotlib.markers import MarkerStyle
list1 = [0, 1] #| x coordinates
list2 = [0, 0] #| y coordinates
p1 = [.5, 0] #| p1 stands for point1, the marker will be placed in this point
marker1 = MarkerStyle(r'$|||$')
marker1._transform.rotate_deg(45)
plt.plot(list1, list2, 'o-k', linewidth = 1)
plt.plot(p1[0], p1[1], 'k', marker = marker1)
#T# show the results
plt.show()
This outputs the following diagram

As can be seen, the marker is rotated 45 degrees, this marker of three small parallel lines is very common in geometric diagrams. The problem here is that I had to directly manipulate a private attribute, instead of doing that through a specific function for that purpose, such as a setter function.
Matplotlib should be capable of producing geometric diagrams, rotating markers is a basic necessity, and as I said before, I wish there could be a way to place the markers perpendicular or parallel to the line in which they are placed, so that the user wouldn't have to calculate the angle of rotation, but this isn't too bad, at least rotating the marker is necessary.
Proposed Solution
Add a wrapper function that acts as a setter for the rotation of markers. The issue #2478 probably has a better way than what I could come up with.

Activity
mwaskom commentedon Dec 31, 2020
I'm not sure if arbitrary markers can be rotated, but markers can take a generic
(numsides, style, angle)tuple that seems to work for your purposes:Polirecyliente commentedon Dec 31, 2020
@mwaskom no good sir, what you said is wrong, I challenge you to reproduce my image with your method... or don't waste your time and I explain you why, the tuple you used creates a regular polygon as a marker, i.e. a line (as you did) an equilateral triangle, a square, pentagon, hexagon, heptagon, etcetera... so you are not far, because if this tuple also accepted a "text" argument, them we would be set, in fact that could be a solution, add a "text" argument to this tuple, that rotates any given text as a marker.
jklymak commentedon Dec 31, 2020
I don't see any reason not to add (with proper documentation and tests of course)
to
marker.py, and indeed I don't see why #2478 was so convoluted and didn't use the transform.(Also, please @Polirecyliente, lets try not to "challenge" people who are trying to help!)
mwaskom commentedon Dec 31, 2020
The problem in your example appeared to be placing small tick marks on a plot with arbitrary rotation. The more complicated marker is just three tick marks with the same angle and small offsets from each other. While not as simple as a single marker with a rotation parameter, that could solve your problem immediately rather than waiting on a change in matplotlib. One can also imagine writing a small function that abstracts the drawing of a triple tick mark in your code. But as you say, it seems like offering further helpful suggestions might be a waste of time...
tacaswell commentedon Dec 31, 2020
I think @jklymak suggestion of adding a rotation setting (and letting it come in through the
__init__) is a step in the right direction (which will handle cases where we want to have the same rotation on every marker in either plot or scatter), but I also think we should revive #2478 again (which will handle the case where we want each marker in a collection to be rotated differently like we do in quiver / windbarb).attn @brunobeltran
jklymak commentedon Dec 31, 2020
Is this something @brunobeltran was working on?
A very quick look at
collections.pyseems there are quite a few ways it could be modernized, likely by just making the top-level class always have aself._transformsproperty, and making it a list of real transforms (sometimes it is just nx3x3 matrix). That may be a little wasteful for some artists that can't use it, but they could always zero it out. OddlyCollections.__init__does not define a placeholder, butCollections.set_sizesetsself._transform.Once that is all done, it seems
set_rotationsis an easy next step.tacaswell commentedon Jan 1, 2021
Bruno has been doing a bunch of work on the
Markerclass.Polirecyliente commentedon Jan 1, 2021
For the sake of completion, I must say that
rotate_degis not the only marker function that is private, there is alsoskew,translate,scale, androtate(in radians), all those should be included, they are useful, I've used bothrotate_degandscale, and all of these are methods of the_transformproperty of markers.jklymak commentedon Jan 1, 2021
Right, its the transform that is private, but passing around transforms can be problematic. But we could possibly take a transform argument when we make the marker.
timhoffm commentedon Jan 1, 2021
That sounds reasonable. I'd add the limitation that a passed transform should not be modified afterwards (e.g. via
set_matrix). I suppose we might want to combine the passed transform and the internal_transformand we don't want to keep track of both and possible updates. Also, changing marker transforms later will be an even more rare use case than just transforming markers at all.The API would be something like
which is a bit bulky but IMHO still ok and it lets you specify any transform you want.
jklymak commentedon Jul 9, 2021
#20613 (comment) also makes the case that
joinstyleshould have a public interface.jklymak commentedon Jul 9, 2021
marking as a "Good First Issue". Getting the API change right is a bit tricky, but not technically difficult. It will require tests, and ideally additional documentation wherever
MarkerStyleis discussed in depth.Kuldeep-kd commentedon Aug 21, 2021
I would be very happy to contribute to this feature. From the above discussion I am thinking of two implementations,
append_transform(**kwargs)orset_transform(arg).skew,translate,scale,rotate, etc in the "matplotlib/markers.py" itself.This will be my first contribution, I appreciate any help, if I have misunderstood anything or any suggestion will be great.
deep-jkl commentedon Aug 26, 2021
Hi, sorry to interrupt, I was slowly working on this issue last couple of weeks. I was slowed down by some issues with copying/deepcopying of Path.
j-bowhay commentedon Oct 29, 2021
Has #20914 resolved this?
deep-jkl commentedon Oct 29, 2021
I believe so.
QuLogic commentedon Oct 30, 2021
Closing then, as that PR fixed it.