0

I recently had to re-install my OS and decided to switch to Python3. With it came updates of my IDE PyCharm and presumably also an update of Matplotlib.

Running a script that worked perfectly fine before, now gives me ugly results with overlapping titles of my subplots.

This is an example code:

import numpy as np
import matplotlib.pyplot as plt

z = np.random.uniform(low=0, high=100, size=(20,4))

fig, axes = plt.subplots(2, 2, constrained_layout=True, sharey=True, sharex=True)
axes[-1, 0].set_xlabel('.\n', color=(0, 0, 0, 0))
axes[-1, 0].set_ylabel('.\n', color=(0, 0, 0, 0))
for s_plot, ax in enumerate(axes.flat):
    ax.scatter(x=range(20), y=z[:,s_plot])

fig.suptitle("The Title\nSecond Line\n", fontsize=12)
plt.show()

This produces: enter image description here

I tried setting constrained_layout to False and also experimented with subplots_adjust, but it does not change the layout of my plots.

I am currently using matplotlib 3.0.2. Was there a major change I have missed? I am puzzled about how to solve this.

2 Answers 2

2

Using matplotlib 3.0.2 the plot would look as follows

Using constrained_layout=True enter image description here

Using constrained_layout=False enter image description here

Both outcomes are expected. In the case of constrained_layout being used the title appears off-center, because there is more space to the left of the subplots being used by labels than on the right.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your effort. Any idea why I am getting the result shown in the question?
Can you run the code outside of pycharm? Would it produce the same?
Good point. I saved the plot using plt.savefig and everything looks fine. It seems to be a problem with the PyCharm update, not the matplotlib update. Sorry and thanks!
Mhh, it seems PyCharm is often the cause for trouble with matplotlib. They use their own backend by default. So maybe that causes hickups.
0

I also think this is a problem with pycharm. So for example when I run the code in this standard matplotlib script: https://matplotlib.org/3.1.1/gallery/subplots_axes_and_figures/figure_title.html

I get this image where the title and suptitle overlap: enter image description here

However the title and suptitle do not overlap when saved to png.

I have raised an issue with pycharm to hopefully get this resolved: https://youtrack.jetbrains.com/issue/PY-42545

In the meantime I suggest splitting your editor screen to display the .png file which you can refresh using CTRL+ALT+Y every time you run the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.