vas*_*ek1 466 python matplotlib
我正在Matplotlib中创建一个像这样的人物:
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
fig.savefig('test.jpg')
Run Code Online (Sandbox Code Playgroud)
我想指定图标题和轴标签的字体大小.我需要三个不同的字体大小,所以设置全局字体大小(mpl.rcParams['font.size']=x)不是我想要的.如何单独设置图标题和轴标签的字体大小?
Ava*_*ris 684
处理像label,title等等文本的函数接受与matplotlib.text.Text相同的参数.对于您可以使用的字体大小size/fontsize:
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(data)
fig.suptitle('test title', fontsize=20)
plt.xlabel('xlabel', fontsize=18)
plt.ylabel('ylabel', fontsize=16)
fig.savefig('test.jpg')
Run Code Online (Sandbox Code Playgroud)
对于全局设置title和label大小,mpl.rcParams包含axes.titlesize和axes.labelsize.(来自页面):
axes.titlesize : large # fontsize of the axes title
axes.labelsize : medium # fontsize of the x any y labels
Run Code Online (Sandbox Code Playgroud)
(据我所见,没有办法单独设置x和y标注尺寸.)
而且我看到这axes.titlesize并不影响suptitle.我想,你需要手动设置.
tsa*_*ndo 81
您也可以通过rcParams字典全局执行此操作:
import matplotlib.pylab as pylab
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
Run Code Online (Sandbox Code Playgroud)
spi*_*nup 49
如果您更习惯使用ax对象进行绘图,则可能会ax.xaxis.label.set_size()发现使用ipython终端中的tab更容易记住,或者至少更容易找到.看来效果似乎需要重绘操作.例如:
import matplotlib.pyplot as plt
# set up a plot with dummy data
fig, ax = plt.subplots()
x = [0, 1, 2]
y = [0, 3, 9]
ax.plot(x,y)
# title and labels, setting initial sizes
fig.suptitle('test title', fontsize=12)
ax.set_xlabel('xlabel', fontsize=10)
ax.set_ylabel('ylabel', fontsize='medium') # relative to plt.rcParams['font.size']
# setting label sizes after creation
ax.xaxis.label.set_size(20)
plt.draw()
Run Code Online (Sandbox Code Playgroud)
我不知道在创建之后设置suptitle大小的类似方法.
Wil*_*ler 28
根据官方指南,
pylab不再推荐使用。matplotlib.pyplot应该直接使用。
rcParams应该通过以下方式全局设置字体大小
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 16
plt.rcParams['axes.titlesize'] = 16
# or
params = {'axes.labelsize': 16,
'axes.titlesize': 16}
plt.rcParams.update(params)
# or
import matplotlib as mpl
mpl.rc('axes', labelsize=16, titlesize=16)
# or
axes = {'labelsize': 16,
'titlesize': 16}
mpl.rc('axes', **axes)
Run Code Online (Sandbox Code Playgroud)
可以使用恢复默认值
plt.rcParams.update(plt.rcParamsDefault)
Run Code Online (Sandbox Code Playgroud)
你也可以通过在matplotlib 配置目录下的目录中创建一个样式表来做到这一点(你可以从 中获取你的配置目录)。样式表格式为stylelibmatplotlib.get_configdir()
axes.labelsize: 16
axes.titlesize: 16
Run Code Online (Sandbox Code Playgroud)
如果您有样式表,/path/to/mpl_configdir/stylelib/mystyle.mplstyle则可以通过以下方式使用它
plt.style.use('mystyle')
# or, for a single section
with plt.style.context('mystyle'):
# ...
Run Code Online (Sandbox Code Playgroud)
您还可以创建(或修改)共享格式的matplotlibrc 文件
axes.labelsize = 16
axes.titlesize = 16
Run Code Online (Sandbox Code Playgroud)
根据您修改的 matplotlibrc 文件,这些更改将仅用于当前工作目录、所有没有matplotlibrc 文件的工作目录,或用于没有matplotlibrc 文件且没有其他 matplotlibrc 文件的所有工作目录被指定。有关更多详细信息,请参阅自定义 matplotlib 页面的这一部分。
rcParams可以通过 检索完整的键列表plt.rcParams.keys(),但是为了调整您拥有的字体大小(此处引用斜体)
axes.labelsize- x 和 y 标签的字体大小axes.titlesize-轴标题的字体大小figure.titlesize-图形标题的大小 ( Figure.suptitle())xtick.labelsize-刻度标签的字体大小ytick.labelsize-刻度标签的字体大小legend.fontsize- 图例的字体大小 ( plt.legend(), fig.legend())legend.title_fontsize- 图例标题的字体大小,None设置为与默认轴相同。有关用法示例,请参阅此答案。所有这些都接受字符串大小{'xx-small', 'x-small', 'smaller', 'small', 'medium', 'large', 'larger', 'x-large', 'xxlarge'}或 a floatin pt。字符串大小是相对于由指定的默认字体大小定义的
font.size-文本的默认字体大小,以 pts 为单位。10 pt 是标准值此外,重量可以通过
font.weight- 使用的字体的默认粗细text.Text。接受{100, 200, 300, 400, 500, 600, 700, 800, 900}或'normal'(400), 'bold'(700) 'lighter', 和'bolder'(相对于当前权重)。jef*_*ale 23
如果您没有显式创建图形和轴对象,则可以在使用参数创建标题时设置标题字体大小fontdict。
当您使用参数创建 x 和 y 标签时,可以单独设置 x 和 y 标签字体大小fontsize。
例如:
plt.title('Car Prices are Increasing', fontdict={'fontsize':20})
plt.xlabel('Year', fontsize=18)
plt.ylabel('Price', fontsize=16)
Run Code Online (Sandbox Code Playgroud)
也适用于seaborn 和pandas 绘图(当Matplotlib 是后端时)!
tam*_*moj 10
为了只修改标题的字体(而不是轴的字体),我使用了以下命令:
import matplotlib.pyplot as plt
fig = plt.Figure()
ax = fig.add_subplot(111)
ax.set_title('My Title', fontdict={'fontsize': 8, 'fontweight': 'medium'})
Run Code Online (Sandbox Code Playgroud)
fontdict除了来自matplotlib.text.Text的所有kwarg之外。
Others have provided answers for how to change the title size, but as for the axes tick label size, you can also use the set_tick_params method.
E.g., to make the x-axis tick label size small:
ax.xaxis.set_tick_params(labelsize='small')
Run Code Online (Sandbox Code Playgroud)
or, to make the y-axis tick label large:
ax.yaxis.set_tick_params(labelsize='large')
Run Code Online (Sandbox Code Playgroud)
You can also enter the labelsize as a float, or any of the following string options: 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', or 'xx-large'.