Issue
I do realize this has already been addressed here (e.g.,.how to set local rcParams or rcParams for one figure in matplotlib) Nevertheless, I hope this question was different.
I have a plotting function in python with matplotlib
that includes global properties
, so all new plots will have updated with the global properties
.
def catscatter(data,colx,coly,cols,color=['grey','black'],ratio=10,font='Helvetica',save=False,save_name='Default'):
'''
This function creates a scatter plot for categorical variables. It's useful to compare two lists with elements in common.
'''
df = data.copy()
# Create a dict to encode the categeories into numbers (sorted)
colx_codes=dict(zip(df[colx].sort_values().unique(),range(len(df[colx].unique()))))
coly_codes=dict(zip(df[coly].sort_values(ascending=False).unique(),range(len(df[coly].unique()))))
# Apply the encoding
df[colx]=df[colx].apply(lambda x: colx_codes[x])
df[coly]=df[coly].apply(lambda x: coly_codes[x])
# Prepare the aspect of the plot
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
plt.rcParams['font.sans-serif']=font
plt.rcParams['xtick.color']=color[-1]
plt.rcParams['ytick.color']=color[-1]
plt.box(False)
# Plot all the lines for the background
for num in range(len(coly_codes)):
plt.hlines(num,-1,len(colx_codes),linestyle='dashed',linewidth=2,color=color[num%2],alpha=0.5)
for num in range(len(colx_codes)):
plt.vlines(num,-1,len(coly_codes),linestyle='dashed',linewidth=2,color=color[num%2],alpha=0.5)
# Plot the scatter plot with the numbers
plt.scatter(df[colx],
df[coly],
s=df[cols]*ratio,
zorder=2,
color=color[-1])
# Change the ticks numbers to categories and limit them
plt.xticks(ticks=list(colx_codes.values()),labels=colx_codes.keys(),rotation=90)
plt.yticks(ticks=list(coly_codes.values()),labels=coly_codes.keys())
# Save if wanted
if save:
plt.savefig(save_name+'.png')
Below are the properties that I'm using inside the function,
plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
I want these properties
to apply only when I call the catscatter
function.
Is there a way to set the plotting global properties
specifically for just one figure, without impacting other plots in jupyter notebook
?
Or is there at least a good way to change the properties for one plotting function and then change them back to the values that were used before (not necessarily the rcdefaults
?
Solution
To only change the properties of one figure, you can just use the relevant method on the Figure or Axes instance rather than using the rcParams
.
In this case, it looks like you want to set the x-axis label and ticks to appear on the top of the plot rather than the bottom. You can use the following to achieve exactly that.
ax.xaxis.set_label_position('top')
ax.xaxis.set_ticks_position('top')
Consider the following minimal example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_xlabel('label')
ax.xaxis.set_label_position('top')
ax.xaxis.set_ticks_position('top')
Answered By - tmdavison
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.