I am trying to build a summary_plot using SHAP for multilabel problem, but labels overlap the plot, how can I fix that? plt.subplots_adjust() didn't help.
you can try adjusting the margins of the plot using the tight_layout()
adjust the layout of the subplots by increasing the bottom margin or decreasing the height of the subplots.
plt.tight_layout()
plt.subplots_adjust(bottom=0.2)
plt.subplots_adjust(bottom=0.2) is used to increase the bottom margin of the subplots to create more space for the labels. You can adjust the value of bottom to get the desired layout. You can also adjust other parameters of subplots_adjust such as top, left, right, and hspace to adjust the layout of the subplots.
Related
I got a general issue with a plotting function using matplotlib.pyplot. I plot a set of stacked horizontal bar charts, the goal is to produce congruent figures, the embedded grid is suppose to fit, but the image gets resized, when I change the input data. However, if i plot the image without the legend, I receive the desired output. But I do want to include a legend of course !
The formatting lines I include are the following:
figure size
fig,ax = plt.subplots(1,figsize=(20,9))
grid definition
plt.grid(color="darkgrey")
legend defintion
ax.legend([handles[i] for i in order], [labels[i] for i in order],
bbox_to_anchor=((0.5, -0.15)), loc="upper center",fontsize="medium",ncol=5)
tight layout
fig.tight_layout()
store the image
fig.savefig(str(figname)+'.jpg',bbox_inches='tight',quality=95,dpi=300)
I created a stacked barchart using matplotlib.pyplot but there is no border around the graph so the title of the graph and axes are right up against the edge of the image and get cutoff in some contexts when I use it. I would like to add a small clear or white border around the graph, axes and title. repos_amount is a pandas DataFrame.
Here is my code:
colors = ["Green", "Red","Blue"]
repos_amount[['Agency','MBS','Treasury']].plot.bar(stacked=True, color=colors, figsize=(15,7))
plt.title('Total Outstanding Fed Repos Operations', fontsize=16)
plt.ylabel('$ Billions', fontsize=12)
Here is what the graph looks like:
I tried the suggestions from the link below and I could not figure out how to make it work. I'm not good with matplotlib yet so I would need help figuring out how to apply it to my code.
How to draw a frame on a matplotlib figure
Try adding plt.tight_layout() to the bottom of your code.
Documentation indicates that this tries to fit the titles, labels etc within the subplot figure size, rather than adding items around this figure size.
It can have undesirable results if your labels or headings are too big, in which case you would then need to look into the answers in this thread to adjust the specific box size of your chart elements.
When generating a bokeh plot using python with many categories, for instance a bar plot, the legend will not fit entirely in the screen and it is not possible to scroll.
Is there a way to scroll a legend?
Thank you
There is no scroll option for the legend.
Some suggestions from this answer:
The legend is drawn on the same canvas as the plot, so you could show more of your legend by increasing the plot size.
You could decrease the legend font size with p.legend.label_text_font_size = "8px"
There is a method to resize the entire Figure
plt.gcf().set_size_inches
Is it possible to similarly resize just a subplot of a Figure, i.e. Axis?
My xticks currently overlap and I would like to resize the plot accordingly, to:
len(ticks) * max(tick_lengths)
The problem with using set_size_inches is:
a) it resizes all subplots when a Figure contains more than a single Axis
b) the margins around the Axis and between them are also resized
You can change the spacing of the subplots with (amongst other things):
subplots_adjust(left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
For figures with multiple subplots, you might need to change wspace or hspace. See the documentation for details.
Alternatively, you could try calling plt.tight_layout() after plotting, usually this solves any spacing/overlapping/... problems.
I'm trying to draw an arrow into a loglog plot with matplotlib, which looks like this:
I know that it has been suggested to turn off the axis (Matplotlib: Draw a vertical arrow in a log-log plot), but I do need the axes. In addition, the suggestion did not seem to change anything (apart from turning the axes off, as expected):
plt.figure();plt.loglog([1,10,60],[1,0.1,0.005])
plt.axis('off')
plt.arrow(2,0.002,5,0.098,'k',head_length=0.3)
My work around so far has been to create an invisible inset (meaning: axes off) with a linear axes environment and plot the arrow in the inset, which works but is really a bit unpleasant. Is there a simpler way? Or do people recommend to add these type of additional features with eg. inkscape, after the main plot is done?
You can use plt.annotate rather than plt.arrow. This is noted in the documentation for plt.arrow:
The resulting arrow is affected by the axes aspect ratio and limits.
This may produce an arrow whose head is not square with its stem. To
create an arrow whose head is square with its stem, use annotate()
For example:
import matplotlib.pyplot as plt
plt.figure()
plt.loglog([1,10,60],[1,0.1,0.005])
plt.annotate('', xy=(5, 0.098), xytext=(2, 0.002),
arrowprops=dict(facecolor='black', shrink=0.),
)
plt.ylim(0.001, 10)
plt.show()
Note that you may need to adjust the axes limits to fit the arrow into the plot. Here I had to change ylim.