您一起创建了图形和坐标轴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]中的浮点值的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'])的索引;该索引会在创建艺术家时发生,如果循环中不包含颜色,则默认为黑色. 除"CN"外,所有颜色的字符串规范均不区分大小写.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? 解决方案 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 togetherfig, ax = plt.subplots(nrows=1, ncols=1)You created a figure, then axis/es laterfig = plt.figure()ax = fig.add_subplot(1, 1, 1) # nrows, ncols, indexYou 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()Then you can use 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.colors Matplotlib recognizes the following formats to specify a color: 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. All string specifications of color, other than "CN", are case-insensitive. 这篇关于如何更改情节背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 15:34
查看更多