【问题标题】:Background color of MatplotlibMatplotlib 的背景颜色
【发布时间】:2021-05-20 23:40:09
【问题描述】:
# use a gray background
ax = plt.axes(facecolor='#E6E6E6')
ax.set_axisbelow(True)

# draw solid white grid lines
plt.grid(color='w', linestyle='solid')

# hide axis spines
for spine in ax.spines.values():
    spine.set_visible(True)
    
# hide top and right ticks
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()

# lighten ticks and labels
ax.tick_params(colors='gray', direction='out')
for tick in ax.get_xticklabels():
    tick.set_color('gray')
for tick in ax.get_yticklabels():
    tick.set_color('gray')
    
# control face and edge color of histogram
ax.hist(x, edgecolor='#E6E6E6', color='#EE6666');

输出:

上面的代码生成附加的图片。怎样才能去掉绕轴的黑框?

【问题讨论】:

    标签: matplotlib colors background


    【解决方案1】:

    图像周围的“黑框”是图形容器的一部分。简短的回答是,改变人物的脸色。 (即在第一行上方添加 fig = plt.figure(facecolor='white')。)

    # THIS LINE CONTROLS THE FIGURE FACECOLOR. YOU CAN USE HEX COLOR RGB OR JUST SPECIFY A COLOR
    fig = plt.figure(facecolor='white')
    
    # use a gray background
    ax = plt.axes(facecolor='#E6E6E6')
    ax.set_axisbelow(True)
    
    # draw solid white grid lines
    plt.grid(color='w', linestyle='solid')
    
    # hide axis spines
    for spine in ax.spines.values():
        spine.set_visible(True)
        
    # hide top and right ticks
    ax.xaxis.tick_bottom()
    ax.yaxis.tick_left()
    
    # lighten ticks and labels
    ax.tick_params(colors='gray', direction='out')
    for tick in ax.get_xticklabels():
        tick.set_color('gray')
    for tick in ax.get_yticklabels():
        tick.set_color('gray')
        
    # control face and edge color of histogram
    ax.hist(x, edgecolor='#E6E6E6', color='#EE6666');
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2016-03-05
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多