Issue
I wrote this code (using this example):
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import seaborn as sns
#sns.set()
#%matplotlib notebook
#plt.style.use('classic')
class_list = ['test1','test2','test3','test4','test5','test6','test7']
data = pd.DataFrame(data=zip(class_list,average_length,num_entries),columns=['Class','Lens','Nums'])
data.set_index('Class', inplace=True)
font_color = '#525252'
hfont = {'fontname':'Calibri'}
#facecolor = '#eaeaf2'
color_red = '#fd625e'
color_blue = '#01b8aa'
index = data.index
column0 = data['Lens']
column1 = data['Nums']
title0 = "Title 1"
title1 = 'Title 2'
fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor, ncols=2,sharey=True)
fig.tight_layout()
axes[0].barh(index, column0, align='center', color=color_red, zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color=color_red, **hfont)
axes[1].barh(index, column1, align='center', color=color_blue, zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color=color_blue, **hfont)
# If you have positive numbers and want to invert the x-axis of the left plot
axes[0].invert_xaxis()
# To show data from highest to lowest
plt.gca().invert_yaxis()
axes[0].set(yticks=data.index, yticklabels=data.index)
axes[0].yaxis.tick_left()
axes[0].tick_params(axis='y', colors='white') # tick color
axes[1].set_xticklabels(['0','20', '40', '60', '80', '100', '120'])
for label in (axes[0].get_xticklabels() + axes[0].get_yticklabels()):
label.set(fontsize=13, color=font_color, **hfont)
for label in (axes[1].get_xticklabels() + axes[1].get_yticklabels()):
label.set(fontsize=13, color=font_color, **hfont)
plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.18, right=0.95)
plt.show()
The output is:
I want to remove all the background colours (so there's no 'Figure 1', no white gridlines and the background is pure white instead of blue-y purple), and then just add a black line on the x and y axis.
When I try to add an x and y axis line with this:
plt.update_xaxes(showline=True, linewidth=2, linecolor='black')
plt.update_yaxes(showline=True, linewidth=2, linecolor='black')
There's no error, but there's no graph either.
Edit 1: Adding
axes[0].set_facecolor('white')
axes[1].set_facecolor('white')
Worked for removing the background colours. I also tried to add x and y axis lines via:
axes[0].axvline(0)
axes[0].axhline(0)
This produces a line mid-way through the bottom bar:
(and 'Figure 1' is still there).
Solution
The parameter facecolor
is responsible for the background color. You can remove it to have white background.
Use set_window_title
to add a new title to the window.
Modified code looks like this:
fig, axes = plt.subplots(figsize=(10,5), ncols=2,sharey=True) # Removed facecolor param
fig.tight_layout()
fig.canvas.set_window_title('Custom Title') # Adds new title to the window
Answered By - kkgarg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.