programing tip

matplotlib의 그림 내에서 각 플롯 된 선에 대해 새 색상을 선택하는 방법은 무엇입니까?

itbloger 2020. 9. 14. 08:10
반응형

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 :

질적 컬러 맵 목록

참고 URL : https://stackoverflow.com/questions/4971269/how-to-pick-a-new-color-for-each-plotted-line-within-a-figure-in-matplotlib

반응형