简体   繁体   中英

Overlapping subplot-title since update of Matplotlib

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: 在此处输入图片说明

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.

Using matplotlib 3.0.2 the plot would look as follows

Using constrained_layout=True 在此处输入图片说明

Using constrained_layout=False 在此处输入图片说明

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.

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: 在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM