import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10)
y = np.sin(x)
plt.plot(x, y, dashes=[10, 5, 20, 5], linewidth=2, color='black')
import matplotlib.pyplot as plt
dashList = [(5,2),(2,5),(4,10),(3,3,2,2),(5,2,20,2)]
# List of Dash styles, each as integers in the format: (first line length, first space length, second line length, second space length...)
# set up the axes to look nice:
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([]) # hide x axis numbers
plt.xlim(0,6) # set x and y axis extents
plt.ylim(-0.5,len(dashList)-0.5)
plt.ylabel("dashList element") # add a label to the y axis
for n in range(0,len(dashList)):
plt.plot([0.5,4],[n,n], color = 'black', linestyle='--', dashes=dashList[n]) # plot a horizontal line using each custom line style
# NB plot.plt draws a line between the following points: ([x0,x1],[y0,y1])
plt.text(4.5,n,dashList[n]) # ...and show the numbers used to generate each custom linestyle
plt.show()
3条答案
按热度按时间ercv8c1e1#
您可以使用
dashes
参数来指定自定义破折号样式,以创建远不止这四种类型。例如:字符串
的数据
dashes
参数是一个整数列表,它指定了虚线和空格的大小:在上面的例子中,有一个10点的破折号,一个5点的空格,一个20点的破折号,和另一个5点的空格,然后重复序列。4xy9mtcn2#
下面是另一个示例,您可以使用它来尝试不同的自定义线条样式(定义为列表'dashList'中的元素),如果您想在图中使用多种不同的自定义线条样式,则可以进行调整:
字符串
的数据
ar5n3qh53#
最新的matplotlib文档(目前未发布)现在包括many custom linestyle examples。下面是一个屏幕截图:
的数据
为了更容易复制粘贴,这里是用于制作该图的部分代码:
字符串