我不明白為什么下面的代碼會回傳兩個子圖。如果我注釋掉第一行 (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)
uj5u.com熱心網友回復:
問題是您沒有將子圖傳遞ax給 in 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)
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387292.html
標籤:Python matplotlib
