PyCharm: How to display multiple plots with plt.show()? [duplicate]

The comments from @SimonFromme are correct:

  • you create the figures and then call plt.show only once
  • this doesn’t have to do with the IDE (possibly except for jupyter)

But to get what you want, you need to explicitly create two images and plot the heatmaps inside each one. By default, it seems that seaborn.heatmap uses only one figure and one axes.

import pandas as pd 
import seaborn as sn
import matplotlib.pyplot as plt

fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()

data = {'A': [45, 31, 42, 35, 39], 
        'B': [38, 31, 26, 28, 33], 
        'C': [10, 15, 17, 21, 12], 
        'D': [9, 14, 16, 22, 141]} 
df = pd.DataFrame(data, columns=['A', 'B'])
corrMatrix = df.corr()
sn.heatmap(corrMatrix, annot=True, ax=ax1)

df = pd.DataFrame(data, columns=['C', 'D'])
corrMatrix = df.corr()
sn.heatmap(corrMatrix, annot=True, ax=ax2)

plt.show()

If one figure with two axes is good enough for you, you can replace the two subplots with

fig, (ax1, ax2) = plt.subplots(ncols=2)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top