matplotlib의 그림 내에서 각 플롯 된 선에 대해 새 색상을 선택하는 방법은 무엇입니까?
플로팅 된 각 선에 대해 색상을 지정하고 싶지 않습니다.
for i in range(20):
ax1.plot(x, y)
이에 대한 이미지를 보면 matplotlib는 각 줄마다 다른 색상을 선택하려고 시도하지만 결국에는 색상을 재사용합니다. 이미 사용한 색상이 반복되는 것을 중지하고 사용할 색상 목록을 제공하고 싶습니다.
1.0+ 버전의 matplotlib에서 사용할 수 있으며 axes.color_cycle
( example 참조 ), 이전 버전 Axes.set_default_color_cycle
( example 참조 )을 사용할 수 있습니다.
나는 일반적 으로이 세 가지 중 세 번째를 사용하고 있으며 1 및 2 버전을 확인했습니다.
from matplotlib.pyplot import cm
import numpy as np
#variable n should be number of curves to plot (I skipped this earlier thinking that it is obvious when looking at picture - sorry my bad mistake xD): n=len(array_of_curves_to_plot)
#version 1:
color=cm.rainbow(np.linspace(0,1,n))
for i,c in zip(range(n),color):
ax1.plot(x, y,c=c)
#or version 2: - faster and better:
color=iter(cm.rainbow(np.linspace(0,1,n)))
c=next(color)
plt.plot(x,y,c=c)
#or version 3:
color=iter(cm.rainbow(np.linspace(0,1,n)))
for i in range(n):
c=next(color)
ax1.plot(x, y,c=c)
3의 예 :
prop_cycle
color_cycle
이 일반화를 위해 1.5에서 더 이상 사용되지 않습니다. http://matplotlib.org/users/whats_new.html#added-axes-prop-cycle-key-to-rcparams
# cycler is a separate package extracted matplotlib.
from cycler import cycler
import matplotlib.pyplot as plt
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b'])))
plt.plot([1, 2])
plt.plot([2, 3])
plt.plot([3, 4])
plt.plot([4, 5])
plt.plot([5, 6])
plt.show()
Also shown in the (now badly named) example: http://matplotlib.org/1.5.1/examples/color/color_cycle_demo.html mentioned at: https://stackoverflow.com/a/4971431/895245
Tested in matplotlib 1.5.1.
I don't know if you can automatically change the color, but you could exploit your loop to generate different colors:
for i in range(20):
ax1.plot(x, y, color = (0, i / 20.0, 0, 1)
In this case, colors will vary from black to 100% green, but you can tune it if you want.
See the matplotlib plot() docs and look for the color
keyword argument.
If you want to feed a list of colors, just make sure that you have a list big enough and then use the index of the loop to select the color
colors = ['r', 'b', ...., 'w']
for i in range(20):
ax1.plot(x, y, color = colors[i])
You can also change the default color cycle in your matplotlibrc
file. If you don't know where that file is, do the following in python:
import matplotlib
matplotlib.matplotlib_fname()
This will show you the path to your currently used matplotlibrc file. In that file you will find amongst many other settings also the one for axes.color.cycle
. Just put in your desired sequence of colors and you will find it in every plot you make. Note that you can also use all valid html color names in matplotlib.
You can use a predefined "qualitative colormap" like this:
from matplotlib.cm import get_cmap
name = "Accent"
cmap = get_cmap(name) # type: matplotlib.colors.ListedColormap
colors = cmap.colors # type: list
axes.set_prop_cycle(color=colors)
Tested on matplotlib 3.0.3. See https://github.com/matplotlib/matplotlib/issues/10840 for discussion on why you can't call axes.set_prop_cycle(color=cmap)
.
A list of predefined qualititative colormaps is available at https://matplotlib.org/gallery/color/colormap_reference.html :
'programing tip' 카테고리의 다른 글
svn diff를 만드는 방법 두 개정 사이에 공백이 아닌 줄 변경 만 표시 (0) | 2020.09.14 |
---|---|
npm을 사용하여 rubygems 용 번 들러처럼 필수 패키지를 설치하거나 업데이트합니다. (0) | 2020.09.14 |
균형 잡힌 나무의 정의 (0) | 2020.09.14 |
nodejs에서 스레드를 만드는 방법 (0) | 2020.09.14 |
Git : 단일 수정 오류가 필요함 (0) | 2020.09.14 |