dekkard's solution to "Python matplotlib legend and title are being cliped "

You have to put fig.tight_layout()<\/code> after all changes to the plot, just before show() or save(), like so:<\/p>\n

import<\/span> matplotlib.pyplot<\/span> as<\/span> plt<\/span>\n\nfig<\/span>,<\/span> ax<\/span> =<\/span> plt<\/span>.<\/span>subplots<\/span>()<\/span>\n\nax<\/span>.<\/span>scatter<\/span>(<\/span> [<\/span>50<\/span>,<\/span> 50<\/span>],[<\/span>10<\/span>,<\/span>40<\/span>],<\/span> c<\/span>=<\/span>'blue'<\/span>,<\/span> s<\/span>=<\/span>200<\/span>,<\/span> label<\/span>=&<\/span>quot<\/span>;<\/span>#1 cats and dogs&quot;)<\/span>\nax<\/span>.<\/span>scatter<\/span>([<\/span>100<\/span>,<\/span>100<\/span>],<\/span> [<\/span>30<\/span>,<\/span>60<\/span>],<\/span> c<\/span>=<\/span>'green'<\/span>,<\/span> s<\/span>=<\/span>200<\/span>,<\/span> label<\/span>=&<\/span>quot<\/span>;<\/span>#2 pigs in mud&quot; )<\/span>\nax<\/span>.<\/span>scatter<\/span>([<\/span>30<\/span>,<\/span>30<\/span>],<\/span> [<\/span>30<\/span>,<\/span>60<\/span>],<\/span> c<\/span>=<\/span>'yellow'<\/span>,<\/span> s<\/span>=<\/span>200<\/span>,<\/span> label<\/span>=&<\/span>quot<\/span>;<\/span>#3 nothing good&quot;)<\/span>\nax<\/span>.<\/span>legend<\/span>(<\/span>bbox_to_anchor<\/span>=<\/span>(<\/span>1.01<\/span>,<\/span>1<\/span>),<\/span> borderaxespad<\/span> =<\/span> 0<\/span>,<\/span>fontsize<\/span>=<\/span>8<\/span> )<\/span>\n\nplt<\/span>.<\/span>title<\/span>(<\/span>'Scatter'<\/span>)<\/span>\nplt<\/span>.<\/span>xlabel<\/span>(<\/span>'categories'<\/span>)<\/span>\nplt<\/span>.<\/span>ylabel<\/span>(<\/span>'values'<\/span>)<\/span>\n\nfig<\/span>.<\/span>tight_layout<\/span>()<\/span> # <<<<\/span>\n\nplt<\/span>.<\/span>show<\/span>()<\/span>\n<\/pre><\/div>
You have to put `fig.tight_layout()` after all changes to the plot, just before `show()`, like show() or save(), like so: import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.scatter( [50, 50],[10,40], c='blue', s=200, label="#1 cats and dogs") ax.scatter([100,100], [30,60], c='green', s=200, label="#2 pigs in mud" ) ax.scatter([30,30], [30,60], c='yellow', s=200, label="#3 nothing good") ax.legend(bbox_to_anchor=(1.01,1), borderaxespad = 0,fontsize=8 ) plt.title('Scatter') plt.xlabel('categories') plt.ylabel('values') fig.tight_layout() # <<< plt.show()
You have to put `fig.tight_layout()` after all changes to the plot, just before `show()`, like so: import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.scatter( [50, 50],[10,40], c='blue', s=200, label="#1 cats and dogs") ax.scatter([100,100], [30,60], c='green', s=200, label="#2 pigs in mud" ) ax.scatter([30,30], [30,60], c='yellow', s=200, label="#3 nothing good") ax.legend(bbox_to_anchor=(1.01,1), borderaxespad = 0,fontsize=8 ) plt.title('Scatter') plt.xlabel('categories') plt.ylabel('values') fig.tight_layout() # <<< plt.show()() Also, as documented in the [Tight Layout Guide](https://matplotlib.org/tutorials/intermediate/tight_layout_guide.html): Note that matplotlib.pyplot.tight_layout() will only adjust the subplot params when it is called. In order to perform this adjustment each time the figure is redrawn, you can call fig.set_tight_layout(True), or, equivalently, set rcParams["figure.autolayout"] (default: False) to True.

User: dekkard

Question: Python matplotlib legend and title are being cliped

Back to question