欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Matplotlib配置和样式表操作——Python实现

Matplotlib配置和样式表操作——Python实现

2025/4/20 5:15:44 来源:https://blog.csdn.net/2301_76574743/article/details/143368016  浏览:    关键词:Matplotlib配置和样式表操作——Python实现

今天在使用Python进行Matplotlib配置和样式表操作时候,发生如下报错:

NameError: name 'IPython_default' is not defined

源代码如下:

import numpy as np
from matplotlib import pyplot as plt
plt.style.available[:5]
def hist_and_lines():np.random.seed(0)fig, ax = plt.subplots(1, 2, figsize=(11, 4))ax[0].hist(np.random.randn(1000))for i in range(3):ax[1].plot(np.random.rand(10))ax[1].legend(['a', 'b', 'c'], loc='lower left')
# Update rcParams with IPython_default
plt.rcParams.update(IPython_default)

这时候需要在plt.rcParams.update(IPython_default)前定义IPython_default,

修改一:

# Define IPython_default with some default settings
IPython_default = {'figure.figsize': (10, 6),'axes.titlesize': 'large','axes.labelsize': 'medium','xtick.labelsize': 'small','ytick.labelsize': 'small','legend.fontsize': 'small','font.family': 'serif'
}

或者作出如下定义:

修改二:


IPython_default = plt.rcParams.copy()

修改代码如下:

import numpy as np
from matplotlib import pyplot as plt
plt.style.available[:5]
#%%
def hist_and_lines():np.random.seed(0)fig, ax = plt.subplots(1, 2, figsize=(11, 4))ax[0].hist(np.random.randn(1000))for i in range(3):ax[1].plot(np.random.rand(10))ax[1].legend(['a', 'b', 'c'], loc='lower left')
#%%
# Define IPython_default with some default settings
IPython_default = {'figure.figsize': (10, 6),'axes.titlesize': 'large','axes.labelsize': 'medium','xtick.labelsize': 'small','ytick.labelsize': 'small','legend.fontsize': 'small','font.family': 'serif'
}# Update rcParams with IPython_default
plt.rcParams.update(IPython_default)
#%%
# Now we can create a plot and see that the styles are changed
hist_and_lines()

成功解决并输出结果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词