如何更改情节背景颜色?

时间:2012-12-30 06:02:10

标签: python matplotlib

我在matplotlib中制作散点图,需要将实际绘图的背景更改为黑色。我知道如何使用以下方法更改绘图的面部颜色:

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

enter image description here

我的问题是这会改变剧情周围空间的颜色。如何更改绘图的实际背景颜色?

8 个答案:

答案 0 :(得分:157)

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

  • 您一起创建了一个数字和轴/ es

    fig, ax = plt.subplots(nrows=1, ncols=1)
    
  • 您创建了一个数字,然后是轴/ es

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

example plot with pink background on the axes

作为什么颜色的复习:

  

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 color survey的名字;前缀为'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”以外的所有颜色的字符串规范都不区分大小写。

答案 1 :(得分:37)

这样的东西?使用axisbg关键字subplot

>>> from matplotlib.figure import Figure
>>> from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
>>> figure = Figure()
>>> canvas = FigureCanvas(figure)
>>> axes = figure.add_subplot(1, 1, 1, axisbg='red')
>>> axes.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x2827e50>]
>>> canvas.print_figure('red-bg.png')

(当然,不是散点图,也不是黑色背景。)

enter image description here

答案 2 :(得分:30)

一种方法是在脚本中手动设置轴背景颜色的默认值(参见Customizing matplotlib):

import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'black'

这与Nick T的方法形成对比,后者改变了特定axes对象的背景颜色。如果您要创建具有相似样式的多个不同图并且不想继续更改不同的axes对象,则重置默认值非常有用。

注意:等效于

fig = plt.figure()
fig.patch.set_facecolor('black')
你的问题中的

是:

plt.rcParams['figure.facecolor'] = 'black'

答案 3 :(得分:11)

如果您已拥有axes个对象,就像在Nick T's answer中一样,您也可以使用

 ax.patch.set_facecolor('black')

答案 4 :(得分:8)

简单的答案:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
$(document).on("change", ".price-sorting", function() {

    var sortingMethod = $(this).val();

    if(sortingMethod == 'l2h')
    {
        sortProductsPriceAscending();
    }
    else if(sortingMethod == 'h2l')
    {
        sortProductsPriceDescending();
    }

});
function sortProductsPriceAscending()
{
    var products = $('.product');
products.sort(function(a, b){ return $(a).data("price")-$(b).data("price")});
$(".products-grid").html(products);

}

function sortProductsPriceDescending()
{
        var products = $('.product');
products.sort(function(a, b){ return $(b).data("price") - $(a).data("price")});
$(".products-grid").html(products);
}
</script>
    </head>
 


    <body>
    <select class="price-sorting type-regular" name="price-sorting">
    <option selected disabled>Default</option>
    <option value="l2h">Low to high</option>
    <option value="h2l">High to low</option>
</select>

 <div class="flex-containershop products-grid">

        
    <div class="productbox">  <a href="#"  data-price="300"> <img class="boximage" src="images/new%20design,jpeg.jpg"></img></a>
        
        <p class="procutboxtext">Price:</p> <p class="price" title="USD">300$</p>
        <p class="procutboxtext">Color:<span class="white"></span> <span class="black"></span></p>
    </div>
    
        
    <div class="productbox">  <a href="#" data-price="6"> <img class="boximage" src="images/new%20design,jpeg.jpg"></img></a>
        
        <p class="procutboxtext">Price:</p> <p class="price" title="USD">6$</p>
        <p class="procutboxtext">Color:<span class="white"></span> <span class="black"></span></p>
    </div>
    
        
    <div class="productbox">  <a href="#" data-price="30"> <img class="boximage" src="images/new%20design,jpeg.jpg"></img></a>
        
        <p class="procutboxtext">Price:</p> <p class="price" title="USD">30$</p>
        <p class="procutboxtext">Color:<span class="white"></span> <span class="black"></span></p>
    </div>
    
        
    <div class="productbox">  <a href="#" data-price="20"> <img class="boximage" src="images/new%20design,jpeg.jpg"></img></a>
        
        <p class="procutboxtext">Price:</p> <p class="price" title="USD">20$</p>
        <p class="procutboxtext">Color:<span class="white"></span> <span class="black"></span></p>
    </div>
    
        

    
    </div>
    </body>
</html>

答案 5 :(得分:7)

其他答案中的一个建议是使用getExternalFilesDir(Environment.DIRECTORY_PICTURES)。但是这已被弃用,并且不适用于MatPlotLib&gt; = v2.0。

还有使用ax.set_axis_bgcolor("red")的建议(适用于MatPlotLib v1.5和v2.2)。虽然这很好用,但对于v2.0 +更简单的解决方案是使用

ax.patch.set_facecolor("red")

答案 6 :(得分:0)

最简单的方法可能是在创建绘图时提供颜色:

fig1 = plt.figure(facecolor=(1, 1, 1))

fig1, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, facecolor=(1, 1, 1))

答案 7 :(得分:0)

除了 NickT 的回答之外,您还可以通过将其设置为“无”来删除背景框,如下所述:https://stackoverflow.com/a/67126649/8669161

import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'none'