Development Tip

matplotlib에 선 스타일 목록이 있습니까?

yourdevel 2020. 11. 17. 21:10
반응형

matplotlib에 선 스타일 목록이 있습니까?


플로팅을 할 스크립트를 작성 중입니다. 각각 고유 한 선 스타일 (색상이 아님)을 사용하여 여러 데이터 시리즈를 플로팅하고 싶습니다. 목록을 쉽게 반복 할 수 있지만 이미 파이썬에서 사용할 수있는 목록이 있습니까?


문서 에 따르면 다음을 수행 하여 찾을 수 있습니다.

from matplotlib import lines
lines.lineStyles.keys()
>>> ['', ' ', 'None', '--', '-.', '-', ':']

마커로도 똑같이 할 수 있습니다.

편집 : 최신 버전 에서는 여전히 동일한 스타일이 있지만 점 / 선 사이의 간격을 변경할 수 있습니다.


plot 선적 서류 비치

http://matplotlib.org/1.5.3/api/pyplot_api.html#matplotlib.pyplot.plot 에는 선 + 마커 스타일 목록이 있습니다.

character description
'-'       solid line style
'--'      dashed line style
'-.'      dash-dot line style
':'       dotted line style
'.'       point marker
','       pixel marker
'o'       circle marker
'v'       triangle_down marker
'^'       triangle_up marker
'<'       triangle_left marker
'>'       triangle_right marker
'1'       tri_down marker
'2'       tri_up marker
'3'       tri_left marker
'4'       tri_right marker
's'       square marker
'p'       pentagon marker
'*'       star marker
'h'       hexagon1 marker
'H'       hexagon2 marker
'+'       plus marker
'x'       x marker
'D'       diamond marker
'd'       thin_diamond marker
'|'       vline marker
'_'       hline marker

이것은 pyplot.plot독 스트링의 일부이므로 다음 을 사용하여 터미널에서도 볼 수 있습니다.

import matplotlib.pyplot as plt
help(plt.plot)

내 경험으로 볼 때 플로팅 할 때 색상과 마커를 목록에 포함하는 것이 좋습니다.

이러한 목록을 얻는 방법은 다음과 같습니다. 파헤 치기가 좀 필요했습니다.

import matplotlib
colors_array = matplotlib.colors.cnames.keys()
markers_array = matplotlib.markers.MarkerStyle.markers.keys()

python3에서 .keys()메소드는 반환 dict_keys객체가 아닌를 list. list()python2 에서처럼 배열을 인덱싱 할 수 있으려면 결과를 전달해야합니다 . 예 : 이 SO 질문

따라서 선, 색상 및 마커에 대한 반복 가능한 배열을 만들려면 다음과 같이 사용할 수 있습니다.

import matplotlib
colors_array = list(matplotlib.colors.cnames.keys())
lines_array = list(matplotlib.lines.lineStyles.keys())
markers_array = list(matplotlib.markers.MarkerStyle.markers.keys())

목록에 액세스하는 질문에 직접 대답하는 것은 아니지만이 페이지에 한 가지 더 대안이있는 것이 유용합니다. 점선 스타일을 생성하는 추가 방법이 있습니다.

다음과 같은 가로 줄무늬로 A와 B 사이에 선을 생성 할 수 있습니다.

A |||||||||||||||||||||||||||||||||||||||||||||||

A || || || || || || || || || || || || || || || ||

A | | | | | | | | | | | | | | | | | | | | | | | | B

by increasing the width of your line and specifying a custom dash pattern:

ax.plot(x, y, dashes=[30, 5, 10, 5])

The documentation for matplotlib.lines.Line2D says this about set_dashes(seq):

Set the dash sequence, sequence of dashes with on off ink in points. If seq is empty or if seq = (None, None), the linestyle will be set to solid.

ACCEPTS: sequence of on/off ink in points

I think it could have been said better: as it paints the line, the sequence of numbers specifies how many points are painted along the line, then how many points are left out (in case there are two numbers), how many are painted, how many are unpainted (in case of four numbers in the sequence). With four numbers, you can generate a dash‒dotted line: [30, 5, 3, 5] gives a 30-point-long dash, a 5-point gap, a 3-point dash (a dot), and a 5-point gap. Then it repeats.

This page of the documentation explains this possibility. Look here for two different ways of doing it.


You can copy the dictionary from the Linestyle example:

from collections import OrderedDict

linestyles = OrderedDict(
    [('solid',               (0, ())),
     ('loosely dotted',      (0, (1, 10))),
     ('dotted',              (0, (1, 5))),
     ('densely dotted',      (0, (1, 1))),

     ('loosely dashed',      (0, (5, 10))),
     ('dashed',              (0, (5, 5))),
     ('densely dashed',      (0, (5, 1))),

     ('loosely dashdotted',  (0, (3, 10, 1, 10))),
     ('dashdotted',          (0, (3, 5, 1, 5))),
     ('densely dashdotted',  (0, (3, 1, 1, 1))),

     ('loosely dashdotdotted', (0, (3, 10, 1, 10, 1, 10))),
     ('dashdotdotted',         (0, (3, 5, 1, 5, 1, 5))),
     ('densely dashdotdotted', (0, (3, 1, 1, 1, 1, 1)))])

You can then iterate over the linestyles

fig, ax = plt.subplots()

X, Y = np.linspace(0, 100, 10), np.zeros(10)
for i, (name, linestyle) in enumerate(linestyles.items()):
    ax.plot(X, Y+i, linestyle=linestyle, linewidth=1.5, color='black')

ax.set_ylim(-0.5, len(linestyles)-0.5)

plt.show()

Or you just take a single linestyle out of those,

ax.plot([0,100], [0,1], linestyle=linestyles['loosely dashdotdotted'])

참고URL : https://stackoverflow.com/questions/13359951/is-there-a-list-of-line-styles-in-matplotlib

반응형