Home > Net >  Get one too many subplots in matplotlib
Get one too many subplots in matplotlib

Time:12-20

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)

CodePudding user response:

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

  • Related