python matplotlib dash-dot-dot - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14710221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:12:50  来源:igfitidea点击:

python matplotlib dash-dot-dot - how to?

pythonmatplotliblinestyle

提问by Schorsch

I am using python and matplotlib to generate graphical output.
Is there a simple way to generate a dash-dot-dot line-style?
I am aware of the '--', '-.', and ':'options. Unfortunately, '-..'does not result in a dash-dot-dot line.
I have looked at the set_dashescommand, but that seems to control the length of the dashes and the space between two adjacent dashes.
One option may be to plot two lines on top of each other; one dashed with ample space between the dashes - and one dotted, with the dots as large as the dashes are wide and spaced so that two dots are in between each of the dashes. I do not doubt this can be done, I am simply hoping for an easier way.
Did I overlook an option?

我正在使用 python 和 matplotlib 来生成图形输出。
有没有一种简单的方法来生成点划线样式?
我知道的'--''-.'':'选项。不幸的是,'-..'不会产生点划线。
我看过set_dashes命令,但这似乎控制了破折号的长度和两个相邻破折号之间的空间。
一种选择可能是在彼此之上绘制两条线;一个虚线在虚线之间有足够的空间 - 另一个虚线,与虚线一样大的点宽且间隔开,因此每个虚线之间有两个点。我不怀疑这可以做到,我只是希望有一种更简单的方法。
我是否忽略了一个选项?

采纳答案by unutbu

You can define custom dashes:

您可以定义自定义破折号

import matplotlib.pyplot as plt

line, = plt.plot([1,5,2,4], '-')
line.set_dashes([8, 4, 2, 4, 2, 4]) 
plt.show()

enter image description here

在此处输入图片说明

[8, 4, 2, 4, 2, 4]means

[8, 4, 2, 4, 2, 4]方法

  • 8 points on, (dash)
  • 4 points off,
  • 2 points on, (dot)
  • 4 points off,
  • 2 points on, (dot)
  • 4 points off.
  • 8 分,(破折号)
  • 减4分,
  • 2 点,(点)
  • 减4分,
  • 2 点,(点)
  • 扣4分。


@Achim noted you can also specify the dashesparameter:

@Achim 指出您还可以指定dashes参数:

plt.plot([1,5,2,4], '-', dashes=[8, 4, 2, 4, 2, 4])
plt.show()

produces the same result shown above.

产生如上所示的相同结果。