如何更改绘图背景颜色? [英] How to change plot background color?
问题描述
我正在 matplotlib 中制作散点图,需要将实际图的背景更改为黑色.我知道如何使用以下方法更改绘图的面部颜色:
I am making a scatter plot in matplotlib and need to change the background of the actual plot to black. I know how to change the face color of the plot using:
fig = plt.figure()
fig.patch.set_facecolor('xkcd:mint green')
我的问题是这会改变情节周围空间的颜色.如何更改绘图的实际背景颜色?
My issue is that this changes the color of the space around the plot. How to I change the actual background color of the plot?
推荐答案
使用axes
对象的set_facecolor(color)
方法,您已通过以下方式之一创建:
Use the set_facecolor(color)
method of the axes
object, which you've created one of the following ways:
你一起创建了一个图形和轴
You created a figure and axis/es together
fig, ax = plt.subplots(nrows=1, ncols=1)
您创建了一个图形,然后创建了轴
You created a figure, then axis/es later
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
您使用了有状态 API(如果您要做的不仅仅是几行代码,并且尤其如果您有多个绘图,上面的面向对象方法会让生活更轻松,因为您可以参考具体图形,在某些轴上绘制,也可以自定义)
You used the stateful API (if you're doing anything more than a few lines, and especially if you have multiple plots, the object-oriented methods above make life easier because you can refer to specific figures, plot on certain axes, and customize either)
plt.plot(...)
ax = plt.gca()
然后就可以使用set_facecolor
:
ax.set_facecolor('xkcd:salmon')
ax.set_facecolor((1.0, 0.47, 0.42))
作为颜色可以是什么的复习:
As a refresher for what colors can be:
Matplotlib 识别以下格式来指定颜色:
matplotlib.colors
Matplotlib recognizes the following formats to specify a color:
[0, 1]
中浮点值的 RGB 或 RGBA 元组(例如,(0.1, 0.2, 0.5)
或(0.1, 0.2,0.5, 0.3)
);- 一个十六进制 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 颜色;- CN"颜色规范,即C"后跟一个数字,这是默认属性循环的索引(
matplotlib.rcParams['axes.prop_cycle']
);索引发生在艺术家创作时,如果循环不包括颜色,则默认为黑色.
- an RGB or RGBA tuple of float values in
[0, 1]
(e.g.,(0.1, 0.2, 0.5)
or(0.1, 0.2, 0.5, 0.3)
); - a hex RGB or RGBA string (e.g.,
'#0F0F0F'
or'#0F0F0F0F'
); - a string representation of a float value in
[0, 1]
inclusive for gray level (e.g.,'0.5'
); - one of
{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}
; - a X11/CSS4 color name;
- a name from the xkcd color survey; prefixed with
'xkcd:'
(e.g.,'xkcd:sky blue'
); - one of
{'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'}
which are the Tableau Colors from the ‘T10’ categorical palette (which is the default color cycle); - 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.
颜色的所有字符串规范,除CN"外,不区分大小写.
All string specifications of color, other than "CN", are case-insensitive.
这篇关于如何更改绘图背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!