Matplotlib 子图背景颜色不适用于系列中的最后一张图
Matplotlib subplot Background color not applying to the last graph in series
我正在尝试 set_facecolor() 用于我在 matplotlib 中的子图,出于某种原因,这些属性从未应用于最后一张图,因此没有类似的问题,因此,不得不 post 这个问题。我在 Python 中的代码是(我几周前才开始在 Python 中编码,因此请原谅我的低效代码,首先尝试解决问题)-
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
import numpy as np
city = 'Mumbai'
df = pd.read_excel (r'filename')
gg = df[df['City']==city]
x = gg['Date']
y1 = gg['Parks']
y2 = gg['Transit']
y3 = gg['Workplaces']
y4 = gg['Residential']
y5 = gg['Retail']
y6 = gg['Grocery']
fig,a = plt.subplots(1,6,squeeze=False,figsize=(18,3),sharey=True)
fig.tight_layout(pad=0)
plt.box(on=None)
y = ""
plt.yticks(np.arange(-100, 101, 50))
a[0][0].set_ylim(top=50, bottom = -100)
plt.tight_layout()
plt.subplots_adjust(wspace = 0.033)
for i in range(0,6):
a[0][i].xaxis.set_major_locator(mdates.MonthLocator())
a[0][i].xaxis.set_major_formatter(mdates.DateFormatter("%b"))
a[0][i].spines["top"].set_visible(False)
a[0][i].spines["right"].set_visible(False)
a[0][i].spines["bottom"].set_visible(False)
a[0][i].spines["left"].set_visible(False)
a[0][i].hlines(0,min(x), max(x),color='black', linewidth=0.5)
a[0][i].set_facecolor('lightgrey')
#a[0][i].axes.get_xaxis().set_visible(False)
if(i!=0):
a[0][i].tick_params(left = False)
a[0][0].plot(x,y1)
a[0][1].plot(x,y2)
a[0][2].plot(x,y3)
a[0][3].plot(x,y4)
a[0][4].plot(x,y5)
a[0][5].plot(x,y6)
plt.show()
如您所见,最后一张图的背景色没有 'lightgrey'。
只需在a[0][i].set_facecolor('lightgrey')
.
前面加上a[0][i].set_frame_on(True)
(后面也可以)
我正在尝试 set_facecolor() 用于我在 matplotlib 中的子图,出于某种原因,这些属性从未应用于最后一张图,因此没有类似的问题,因此,不得不 post 这个问题。我在 Python 中的代码是(我几周前才开始在 Python 中编码,因此请原谅我的低效代码,首先尝试解决问题)-
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.dates as mdates
import numpy as np
city = 'Mumbai'
df = pd.read_excel (r'filename')
gg = df[df['City']==city]
x = gg['Date']
y1 = gg['Parks']
y2 = gg['Transit']
y3 = gg['Workplaces']
y4 = gg['Residential']
y5 = gg['Retail']
y6 = gg['Grocery']
fig,a = plt.subplots(1,6,squeeze=False,figsize=(18,3),sharey=True)
fig.tight_layout(pad=0)
plt.box(on=None)
y = ""
plt.yticks(np.arange(-100, 101, 50))
a[0][0].set_ylim(top=50, bottom = -100)
plt.tight_layout()
plt.subplots_adjust(wspace = 0.033)
for i in range(0,6):
a[0][i].xaxis.set_major_locator(mdates.MonthLocator())
a[0][i].xaxis.set_major_formatter(mdates.DateFormatter("%b"))
a[0][i].spines["top"].set_visible(False)
a[0][i].spines["right"].set_visible(False)
a[0][i].spines["bottom"].set_visible(False)
a[0][i].spines["left"].set_visible(False)
a[0][i].hlines(0,min(x), max(x),color='black', linewidth=0.5)
a[0][i].set_facecolor('lightgrey')
#a[0][i].axes.get_xaxis().set_visible(False)
if(i!=0):
a[0][i].tick_params(left = False)
a[0][0].plot(x,y1)
a[0][1].plot(x,y2)
a[0][2].plot(x,y3)
a[0][3].plot(x,y4)
a[0][4].plot(x,y5)
a[0][5].plot(x,y6)
plt.show()
如您所见,最后一张图的背景色没有 'lightgrey'。
只需在a[0][i].set_facecolor('lightgrey')
.
a[0][i].set_frame_on(True)
(后面也可以)