Snofus
Snofus

Reputation: 1

Background color of Matplotlib

# 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');

Output:

enter image description here

The codes above generate the pic attached. How can I get rid of the black frame circulating the axes?

Upvotes: 0

Views: 1318

Answers (1)

Jeffrey vargas
Jeffrey vargas

Reputation: 11

The "black frame" surrounding your image is part of the figure container. The short answer is, change the facecolor of the figure. (i.e. add, fig = plt.figure(facecolor='white') above your first line.)

# 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');

Upvotes: 1

Related Questions