Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get one too many subplots in matplotlib

I can’t figure out why the below code returns two subplots. If I comment out the first line (plt.subplots), I get one single map. But when it’s included I get a huge whitespace first, just as I would if I specified 2 subplots and only put data in the second. I can’t understand why.

This is not a reproducible example, what I’m looking for is if it’s something my code is lacking.

fig, ax = plt.subplots(figsize=(4, 4))
#matplotlib.rcParams["figure.dpi"] = 250
ax.axis('off')

ax=lan.plot(color='lightgrey', edgecolor='black', linewidth=0.3)
ax.set_facecolor("lightskyblue")
ax=geo_df1.plot(edgecolor='black', column=geo_df1.rel_grp, ax=ax, cmap=my_cmp, linewidth=0.3, categorical=True)

fig.show(ax)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is you are not passing the subplot ax to in lan.plot.

Change this line:

ax=lan.plot(color='lightgrey', edgecolor='black', linewidth=0.3)

To this:

ax=lan.plot(color='lightgrey', edgecolor='black', linewidth=0.3, ax=ax)

For example, this code:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame({
   'pig': [20, 18, 489, 675, 1776],
   'horse': [4, 25, 281, 600, 1900]
   }, index=[1990, 1997, 2003, 2009, 2014])

fig, ax = plt.subplots(figsize=(4, 4))
ax.axis('off')

ax = df.plot()
ax = df.plot(ax=ax)

fig.show(ax)

Outputs:

Out1

But changing to this:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.DataFrame({
   'pig': [20, 18, 489, 675, 1776],
   'horse': [4, 25, 281, 600, 1900]
   }, index=[1990, 1997, 2003, 2009, 2014])

fig, ax = plt.subplots(figsize=(4, 4))
ax.axis('off')

ax = df.plot(ax=ax)
ax = df.plot(ax=ax)

fig.show(ax)

Outputs:

Out2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading