Line2D.set_linestyle关于如何使用和在 matplotlib 行中设置自定义破折号有很多问题Line2D.set_dashes。但是,我似乎找不到在设置后检索破折号图案的方法。
这是在主站点上设置破折号的示例,我将在下面引用: http: //matplotlib.org/examples/lines_bars_and_markers/line_demo_dash_control.html。为了清楚起见,将代码复制到此处:
Run Code Online (Sandbox Code Playgroud)""" 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()
没有line.get_dashes办法。调用line.get_linestyle返回'--'。如何使用 MatPlotLib 1.5.1 检索实际的虚线图案?
更新
对于那些关心我为什么问的人:我正在尝试使用该包创建一个从线条样式到 LaTeX 的简单转换器dashrule。最初的灵感来自于这个问题/答案。如果我可以完全避免使用线条样式并直接获取破折号和线宽,那就太好了。
tikz我已经学会了用package制作标记,所以最终能够将图例元素添加到轴标签中会很好。经过更多的研究和努力,我希望将所有内容合并到 MatPlotLib 的 PR 中。
更新#2
, 似乎有一个“私有”属性Line2D,_dashSeq其中包含我正在查找的序列。但是,如果我设置预设样式之一,该属性的值不会改变:例如
>>> 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)
Run Code Online (Sandbox Code Playgroud)
问题是我现在无法区分这两种样式(预设与自定义),所以我的问题仍然存在。
小智 1
我知道这是一个老问题,但当我试图解决类似的问题时,我发现 protected 属性已更改名称(更改为_unscaled_dash_pattern和_dash_pattern),并且完全遵循预期的逻辑。换句话说,我认为问题已经自行解决了。
笔记:
_dash_pattern随着 line_width 缩放。据我所知,这是为了确保点是方形的。例子:
>>> line.set_linestyle((0, (2, 5, 7, 3)))
>>> line.get_linestyle() # Expected behaviour for custom dash
'--'
>>> line._unscaled_dash_pattern # This is what was needed
(0, (2, 5, 7, 3))
>>> line._dash_pattern # This is the scaled version (lw=0.5)
(0.0, [1.0, 2.5, 3.5, 1.5])
>>> line.set_linestyle('--')
>>> line.get_linestyle() # Still expected behaviour
'--'
>>> line._unscaled_dash_pattern # Now contains updated dash pattern
(0.0, (3.7, 1.6))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
700 次 |
| 最近记录: |