<p>使用<code>axes</code>对象的<strong><code>set_facecolor(color)</code>方法,您已经创建了以下方法之一:</p>
<ul>
<li><p>您一起创建了一个图形和轴</p>
<pre><code>fig, ax = plt.subplots(nrows=1, ncols=1)
</code></pre></li>
<li><p>您创建了一个图形,然后稍后创建axis/es</p>
<pre><code>fig = plt.figure()
ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index
</code></pre></li>
<li><p>您使用了有状态API(如果您所做的只是几行,而<em>特别是</em>如果您有多个绘图,那么上面的面向对象方法会使您的工作变得更容易,因为您可以引用特定的图形、在某些轴上绘图和自定义其中一个)</p>
<pre><code>plt.plot(...)
ax = plt.gca()
</code></pre></li>
</ul>
<p>然后您可以使用<code>set_facecolor</code>:</p>
<pre><code>ax.set_facecolor('xkcd:salmon')
ax.set_facecolor((1.0, 0.47, 0.42))
</code></pre>
<p><a href="https://i.stack.imgur.com/2wFc6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2wFc6.png" alt="example plot with pink background on the axes"/></a></p>
<p>作为什么颜色的提神剂:</p>
<blockquote>
<h2><a href="https://matplotlib.org/api/colors_api.html?highlight=color#module-matplotlib.colors" rel="noreferrer">matplotlib.colors</a></h2>
<p>Matplotlib recognizes the following formats to specify a color:</p>
<ul>
<li>an RGB or RGBA tuple of float values in <code>[0, 1]</code> (e.g., <code>(0.1, 0.2, 0.5)</code> or <code>(0.1, 0.2, 0.5, 0.3)</code>);</li>
<li>a hex RGB or RGBA string (e.g., <code>'#0F0F0F'</code> or <code>'#0F0F0F0F'</code>);</li>
<li>a string representation of a float value in <code>[0, 1]</code> inclusive for gray level (e.g., <code>'0.5'</code>);</li>
<li>one of <code>{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}</code>;</li>
<li>a X11/CSS4 color name;</li>
<li>a name from the <a href="https://xkcd.com/color/rgb/" rel="noreferrer">xkcd color survey</a>; prefixed with <code>'xkcd:'</code> (e.g., <code>'xkcd:sky blue'</code>);</li>
<li>one of <code>{'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'}</code> which are the Tableau Colors from the ‘T10’ categorical palette (which is the default color cycle);</li>
<li>a “CN” color spec, i.e. 'C' followed by a single digit, which is an index into the default property cycle (<code>matplotlib.rcParams['axes.prop_cycle']</code>); the indexing occurs at artist creation time and defaults to black if the cycle does not include color.</li>
</ul>
<p>All string specifications of color, other than “CN”, are case-insensitive.</p>
</blockquote>