플롯 배경색을 변경하는 방법은 무엇입니까?
matplotlib에서 산점도를 만들고 실제 그림의 배경을 검은 색으로 변경해야합니다. 다음을 사용하여 플롯의 얼굴 색상을 변경하는 방법을 알고 있습니다.
fig = plt.figure()
fig.patch.set_facecolor('xkcd:mint green')
내 문제는 이것이 플롯 주위의 공간 색상을 변경한다는 것입니다. 플롯의 실제 배경색을 어떻게 변경합니까?
다음 방법 중 하나를 만든 객체 의 set_facecolor(color)
메소드를axes
사용하십시오 .
도형과 축을 함께 만들었습니다
fig, ax = plt.subplots(nrows=1, ncols=1)
그림을 만든 다음 나중에 축을
fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
상태 저장 API를 사용했습니다 (몇 줄 이상을 수행하는 경우 , 특히 여러 플롯이있는 경우 위의 객체 지향 방법을 사용하면 특정 그림을 참조하고 특정 축에 플롯하고 사용자 정의 할 수 있기 때문에보다 편리합니다. 어느 한 쪽)
plt.plot(...) ax = plt.gca()
그런 다음 사용할 수 있습니다 set_facecolor
:
ax.set_facecolor('xkcd:salmon')
ax.set_facecolor((1.0, 0.47, 0.42))
어떤 색상을 사용할 수 있는지에 대한 정보 :
matplotlib.colors
Matplotlib은 다음 형식을 인식하여 색상을 지정합니다.
[0, 1]
(예를 들어,(0.1, 0.2, 0.5)
또는에(0.1, 0.2, 0.5, 0.3)
) 플로트 값의 RGB 또는 RGBA 튜플 ;- 16 진 RGB 또는 RGBA 문자열 (예 :
'#0F0F0F'
또는'#0F0F0F0F'
);[0, 1]
그레이 레벨 (예를 들어,'0.5'
) 을 포함 하는 부동 소수점 값의 문자열 표현 ;- 중 하나
{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}
;- X11 / CSS4 색 이름;
- xkcd 컬러 측량 의 이름 ; 접두사
'xkcd:'
(예를 들어,'xkcd:sky blue'
);- 그 중 하나는
{'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'}
'T10'범주 팔레트 (기본 색상주기)의 Tableau 색상입니다.- a “CN” color spec, i.e. 'C' followed by a single digit, which is an index into the default property cycle (
matplotlib.rcParams['axes.prop_cycle']
); the indexing occurs at artist creation time and defaults to black if the cycle does not include color.All string specifications of color, other than “CN”, are case-insensitive.
Something like this? Use the axisbg
keyword to subplot
:
>>> from matplotlib.figure import Figure
>>> from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
>>> figure = Figure()
>>> canvas = FigureCanvas(figure)
>>> axes = figure.add_subplot(1, 1, 1, axisbg='red')
>>> axes.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x2827e50>]
>>> canvas.print_figure('red-bg.png')
(Granted, not a scatter plot, and not a black background.)
One method is to manually set the default for the axis background color within your script (see Customizing matplotlib):
import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'black'
This is in contrast to Nick T's method which changes the background color for a specific axes
object. Resetting the defaults is useful if you're making multiple different plots with similar styles and don't want to keep changing different axes
objects.
Note: The equivalent for
fig = plt.figure()
fig.patch.set_facecolor('black')
from your question is:
plt.rcParams['figure.facecolor'] = 'black'
If you already have axes
object, just like in Nick T's answer, you can also use
ax.patch.set_facecolor('black')
One suggestion in other answers is to use ax.set_axis_bgcolor("red")
. This however is deprecated, and doesn't work on MatPlotLib >= v2.0.
There is also the suggestion to use ax.patch.set_facecolor("red")
(works on both MatPlotLib v1.5 & v2.2). While this works fine, an even easier solution for v2.0+ is to use
ax.set_facecolor("red")
The easiest thing is probably to provide the color when you create the plot :
fig1 = plt.figure(facecolor=(1, 1, 1))
or
fig1, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, facecolor=(1, 1, 1))
참고 URL : https://stackoverflow.com/questions/14088687/how-to-change-plot-background-color
'Programing' 카테고리의 다른 글
그렇지 않은 경우 파이썬 == vs if! = (0) | 2020.05.23 |
---|---|
모든 재귀를 반복으로 변환 할 수 있습니까? (0) | 2020.05.23 |
Objective C에서 방법 옆의 더하기 및 빼기 기호는 무엇을 의미합니까? (0) | 2020.05.23 |
시퀀스 다이어그램에 "if"조건을 표시하는 방법은 무엇입니까? (0) | 2020.05.23 |
객체 또는 메소드의 Java 동기화 메소드 잠금? (0) | 2020.05.23 |