今天在使用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()
成功解决并输出结果:
标签:plt,Python,random,Matplotlib,default,np,样式表,IPython,ax From: https://blog.csdn.net/2301_76574743/article/details/143368016