如何更改情节背景颜色? [英] How to change plot background color?

查看:67
本文介绍了如何更改情节背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在matplotlib中制作散点图,需要将实际图的背景更改为黑色.我知道如何使用以下方法更改情节的脸色:

fig = plt.figure()
fig.patch.set_facecolor('xkcd:mint green')

我的问题是,这改变了情节周围空间的颜色.如何更改绘图的实际背景颜色?

解决方案

使用axes对象的 set_facecolor(color)方法,您已通过以下方法之一创建了该方法:

  • 您一起创建了图形和坐标轴

    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 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
    

  • 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()
    

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.

这篇关于如何更改情节背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆