【问题标题】:Get one too many subplots in matplotlib在 matplotlib 中获取一个太多的子图
【发布时间】:2022-01-21 21:43:20
【问题描述】:

我不明白为什么下面的代码会返回两个子图。如果我注释掉第一行(plt.subplots),我会得到一张地图。但是当它被包含在内时,我首先得到一个巨大的空白,就像我指定 2 个子图并且只将数据放在第二个中一样。我不明白为什么。

这不是一个可重现的例子,我正在寻找的是我的代码是否缺少它。

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)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    问题是您没有将子图ax 传递给lan.plot

    改变这一行:

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

    到这里:

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

    例如这段代码:

    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)
    

    输出:

    但改成这样:

    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)
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 2015-10-20
      • 2019-09-10
      • 2016-01-02
      • 2015-10-24
      • 2020-03-03
      相关资源
      最近更新 更多