为什么seaborn样式在matplotlib中不生效,是否已不再被支持?
问题解答
你遇到的是Matplotlib版本更新带来的样式接口变动问题:Matplotlib从3.6版本开始移除了原内置的seaborn-*系列样式,该系列样式原本是为了兼容旧版本seaborn设计,因此你原来调用的seaborn-darkgrid在高版本Matplotlib中无法被识别,会自动回退到默认样式。
可用解决方案
- 方案1:直接使用seaborn官方提供的原生样式接口,适配性更高,仅需将原样式设置行替换为seaborn的样式设置方法即可:
# 原plt.style.use('seaborn-darkgrid')替换为下方代码 import seaborn as sns sns.set_style("darkgrid")
- 方案2:使用新版Matplotlib中更名后的兼容样式,将原样式调用行修改为:
plt.style.use('seaborn-v0_8-darkgrid')
- 方案3:如果希望完全复用原有代码无需修改,可将Matplotlib版本降级到3.5.x及以下版本。
修改后可运行的完整代码示例
import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline # 替换为seaborn原生样式设置即可 sns.set_style("darkgrid") dfLOADA = df_dayM[['InfNH3', 'EffNH3']] plt.plot(dfLOADA) plt.title('Influent and Effluent NH3: ' + month) plt.xticks(dfLOADA.index[::7]) plt.legend(dfLOADA.columns) plt.xlabel('Time [days]') plt.ylabel('[mg NH3/L]') plt.show()
内容的提问来源于stack exchange,提问作者blRVA15
相关产品推荐
相关产品推荐

