There are a whole slew of questions on how to set custom dashes in matplotlib lines using Line2D.set_linestyle
and Line2D.set_dashes
. However, I can not seem to find a way of retrieving the dash pattern after it has been set.
Here is an example of setting the dashes on the main site that I will refer to below: http://matplotlib.org/examples/lines_bars_and_markers/line_demo_dash_control.html. Code is copied here for clarity:
""" Demo of a simple plot with a custom dashed line. A Line object's ``set_dashes`` method allows you to specify dashes with a series of on/off lengths (in points). """ import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10) line, = plt.plot(x, np.sin(x), '--', linewidth=2) dashes = [10, 5, 100, 5] # 10 points on, 5 off, 100 on, 5 off line.set_dashes(dashes) plt.show()
There is no line.get_dashes
method. Calling line.get_linestyle
returns '--'
. How do I retrieve the actual dash pattern using MatPlotLib 1.5.1?
UPDATE
For those who care why I am asking: I am trying to create a simple converter from line-style to LaTeX using the dashrule
package. The original inspiration comes from this question/answer. It would be nice if I could avoid using linestyle entirely and just get the dashes and linewidth directly.
I have learned to make markers with the tikz
package, so it would be nice to eventually be able to add legend elements to axis labels. Given a bit more research and elbow-grease, I hope to combine everything into a PR to MatPlotLib.
UPDATE #2
There appears to be a "private" attribute of Line2D
, _dashSeq
, which contains the sequence I am looking for. However, the value of this attribute does not change if I set one of the preset styles: e.g.
>>> line.set_linestyle((0, (2, 5, 7, 3)))
>>> line.get_linestyle() # This seems expected behavior for custom dash
'--'
>>> line._dashSeq # This is what I want
(2, 5, 7, 3)
>>> line.set_linestyle('--')
>>> line.get_linestyle() # This reflects reality now
'--'
>>> line._dashSeq # This no longer does
(2, 5, 7, 3)
The problem is that I now can't tell the two styles (preset vs. custom) apart, so my question still holds.