【问题标题】:How to change the face color of a plot using Matplotlib如何使用 Matplotlib 更改绘图的表面颜色
【发布时间】:2011-03-03 14:12:28
【问题描述】:

我刚开始使用 Matplotlib 并正在尝试更改绘图的面部颜色的颜色...

如果我像这样创建图形:

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

只有图形的边框变成黄色......我想要的是边框是白色的,情节是黄色的......

编辑:

我当前代码的一个片段:

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

ax = plt.gca()

ax.plot(x, y, color = 'g')

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    嗯,你可以试试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')
    

    【讨论】:

    • @Fábio 给出的例子表明你可以使用add_subplot 方法的bgcolor 关键字参数来达到同样的效果。
    【解决方案2】:

    this是你想要的吗?

    #!/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()
    

    【讨论】:

    • 只是为了稍微更新一下,axisbg 参数已被弃用并替换为facecolor
    猜你喜欢
    • 2019-06-29
    • 2013-09-08
    • 2019-11-16
    • 1970-01-01
    • 2014-10-04
    • 2019-03-23
    • 2019-03-29
    • 2020-06-16
    • 2016-03-05
    相关资源
    最近更新 更多