Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize matplotlib plots using gcf() or gca()?

I am using a package called shap which has a integrated plot function. However i want to adjust some things like the labels, legend, coloring, size etc.

apparently due to the developer thats possible via using plt.gcf().

I call the plot like this, this will give a figure object but i am not sure how to use it:

fig = shap.summary_plot(shap_values_DT, data_train,color=plt.get_cmap("tab10"), show=False)
ax = plt.subplot()

enter image description here

UPDATE / SOLUTION Finally i got everything adjusted as i wanted it by doing the following:

shap.summary_plot(shap_values_DT, data_train, color=plt.get_cmap("tab10"), show=False)
fig = plt.gcf()
fig.set_figheight(12)
fig.set_figwidth(14)
ax = plt.gca()
ax.set_xlabel(r'durchschnittliche SHAP Werte $\vert\sigma_{ij}\vert$', fontsize=16)
ax.set_ylabel('Inputparameter', fontsize=16)
ylabels = string_latexer([tick.get_text() for tick in ax.get_yticklabels()])
ax.set_yticklabels(ylabels)
leg = ax.legend()
for l in leg.get_texts(): l.set_text(l.get_text().replace('Class', 'Klasse'))
plt.show()

enter image description here

like image 454
Quastiat Avatar asked Oct 16 '19 14:10

Quastiat


People also ask

What is GCA () in matplotlib?

The gca() function in pyplot module of matplotlib library is used to get the current Axes instance on the current figure matching the given keyword args, or create one. Syntax: matplotlib.pyplot.gca(\*\*kwargs)

How do I customize matplotlib?

There are three ways to customize Matplotlib: Setting rcParams at runtime. Using style sheets. Changing your matplotlibrc file.

What does Fig GCA do?

This module is used to control the default spacing of the subplots and top level container for all plot elements.

What is GCA in pandas?

gca means "get current axes". "Current" here means that it provides a handle to the last active axes. If there is no axes yet, an axes will be created. If you create two subplots, the subplot that is created last is the current one.


1 Answers

Finally i got everything adjusted as i wanted it by doing the following:

shap.summary_plot(shap_values_DT, data_train, color=plt.get_cmap("tab10"), show=False)
fig = plt.gcf()
fig.set_figheight(12)
fig.set_figwidth(14)
ax = plt.gca()
ax.set_xlabel(r'durchschnittliche SHAP Werte $\vert\sigma_{ij}\vert$', fontsize=16)
ax.set_ylabel('Inputparameter', fontsize=16)
ylabels = string_latexer([tick.get_text() for tick in ax.get_yticklabels()])
ax.set_yticklabels(ylabels)
leg = ax.legend()
for l in leg.get_texts(): l.set_text(l.get_text().replace('Class', 'Klasse'))
plt.show()

enter image description here

like image 141
Quastiat Avatar answered Dec 04 '22 17:12

Quastiat