如何使用Matplotlib更改绘图的面部颜色

Ric*_*ard 6 python matplotlib

我刚开始使用Matplotlib,我正试图改变一个情节的面色的颜色......

如果我创建这样的数字:

 plt.figure(num=None, figsize=(5, 10), dpi=80, facecolor='y', edgecolor='k')
Run Code Online (Sandbox Code Playgroud)

只有图中的边框变为黄色...我想要的是白色的边框和黄色的情节..

编辑:

来自我当前代码的剪辑:

plt.figure(num=None, figsize=(5, 10), dpi=80, facecolor='y', edgecolor='k')  

ax = plt.gca()

ax.plot(x, y, color = 'g')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bjö*_*lex 7

嗯,你可以试试set_axis_bgcolor.而且,gca尝试这个,而不是使用它,它更清洁:

fig = plt.figure(num=None, figsize=(5, 10), dpi=80, facecolor='y', edgecolor='k')
ax = fig.add_subplot(111)
ax.set_axis_bgcolor("y")
ax.plot(x, y, color = 'g')
Run Code Online (Sandbox Code Playgroud)


Fáb*_*niz 6

就是这个,你想要什么?

在此输入图像描述

#!/usr/bin/env python
"""
matplotlib gives you 4 ways to specify colors,

    1) as a single letter string, ala MATLAB

    2) as an html style hex string or html color name

    3) as an R,G,B tuple, where R,G,B, range from 0-1

    4) as a string representing a floating point number
       from 0 to 1, corresponding to shades of gray.

See help(colors) for more info.
"""
from pylab import *

subplot(111, axisbg='darkslategray')
#subplot(111, axisbg='#ababab')
t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, 'y')
xlabel('time (s)', color='r')
ylabel('voltage (mV)', color='0.5') # grayscale color
title('About as silly as it gets, folks', color='#afeeee')
show()
Run Code Online (Sandbox Code Playgroud)

  • 为什么这只适用于`subplot`而不是`Figure`? (4认同)