我在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')
我想指定图标题和轴标签的字体大小。我需要三个不同的字体大小,因此设置全局字体大小(mpl.rcParams['font.size']=x
)不是我想要的。如何单独设置图标题和轴标签的字体大小?
答案 0 :(得分:575)
处理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')
对于全局设置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
(据我所知,无法单独设置x
和y
标签尺寸。)
我发现axes.titlesize
不会影响suptitle
。我想,你需要手动设置。
答案 1 :(得分:62)
您也可以通过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)
答案 2 :(得分:29)
如果您更习惯使用ax
个对象进行绘图,您可能会发现ax.xaxis.label.set_size()
更容易记住,或者至少更容易使用ipython终端中的标签查找。看来效果似乎需要重绘操作。例如:
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()
我不知道在创建之后设置suptitle大小的类似方法。
答案 3 :(得分:5)
其他人提供了有关如何更改标题大小的答案,但对于轴刻度标签大小,您也可以使用set_tick_params方法。
例如,使x轴刻度标签的尺寸变小:
ax.xaxis.set_tick_params(labelsize='small')
或者,使y轴刻度标签变大:
ax.yaxis.set_tick_params(labelsize='large')
您也可以输入labelsize
作为浮点数,或以下任意字符串选项:“ xx-small”,“ x-small”,“ small”,“ medium”,“ large”,“ x-large”或“ xx-large”。
答案 4 :(得分:3)
仅修改标题的字体(而不是轴的字体),我使用了它:
import matplotlib.pyplot as plt
fig = plt.Figure()
ax = fig.add_subplot(111)
ax.set_title('My Title', fontdict={'fontsize': 8, 'fontweight': 'medium'})
fontdict,除了matplotlib.text.Text中的所有kwarg。
答案 5 :(得分:1)
将right_ax
放在set_ylabel()
之前
ax.right_ax.set_ylabel('AB scale')
答案 6 :(得分:1)
每the official guide,不再建议使用
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)
可以使用
恢复默认设置plt.rcParams.update(plt.rcParamsDefault)
您还可以通过在style sheet下的stylelib
目录中创建一个matplotlib configuration directory来完成此操作(您可以从matplotlib.get_configdir()
获取配置目录)。样式表格式为
axes.labelsize: 16
axes.titlesize: 16
如果您在/path/to/mpl_configdir/stylelib/mystyle
有样式表,则可以通过
plt.style.use('mystyle')
# or, for a single section
with plt.style.context('mystyle'):
# ...
您还可以创建(或修改)共享格式的matplotlibrc file
axes.labelsize = 16
axes.titlesize = 16
根据您修改的matplotlibrc文件,这些更改将仅用于当前工作目录,没有没有matplotlibrc文件的所有工作目录或所有具有 >没有具有matplotlibrc文件,并且未指定其他matplotlibrc文件。有关更多详细信息,请参见定制matplotlib页面的this section。
可以通过rcParams
检索plt.rcParams.keys()
键的完整列表,但是要调整字体大小(斜体用here引出)
axes.labelsize
- x和y标签的字体大小 axes.titlesize
-轴标题的字体 figure.titlesize
-图形标题的大小(Figure.suptitle()
) xtick.labelsize
-刻度标签的字体 ytick.labelsize
-刻度标签的字体 legend.fontsize
-图例(plt.legend()
,fig.legend()
)的字体大小legend.title_fontsize
-图例标题的字体大小, None
设置为与默认轴相同。用法示例,请参见this answer。所有这些字符都接受字符串大小{'xx-small', 'x-small', 'smaller', 'small', 'medium', 'large', 'larger', 'x-large', 'xxlarge'}
或float
中的pt
。字符串大小是相对于
font.size
-文本的默认字体大小,以磅为单位。标准值是10点 此外,可以通过以下方式指定重量(尽管仅显示默认值)
font.weight
-text.Text
使用的默认字体粗细。接受{100, 200, 300, 400, 500, 600, 700, 800, 900}
或'normal'
(400),'bold'
(700),'lighter'
和'bolder'
(相对于当前权重的 )。答案 7 :(得分:0)
更改字体大小的另一种方法是更改填充。当Python保存您的PNG时,您可以使用打开的对话框更改布局。轴之间的间距,如果需要的话可以填充。
答案 8 :(得分:0)
将 numpy 导入为 np
将 matplotlib.pyplot 导入为 plt
高度 = [3, 12, 5, 18, 45]
条形 = ('A', 'B', 'C', 'D', 'E')
x_pos = np.arange(len(bars))
plt.bar(x_pos, height, color = (0.5,0.1,0.5,0.6))
plt.title('我的标题')
plt.xlabel('categories')
plt.ylabel('values')
plt.xticks(x_pos, bar)
plt.show()
答案 9 :(得分:-5)
from numpy import*
import matplotlib.pyplot as plt
X = linspace(-pi, pi, 1000)
class Crtaj:
def nacrtaj(self,x,y):
self.x=x
self.y=y
return plt.plot (x,y,"om")
def oznaci(self):
return plt.xlabel("x-os"), plt.ylabel("y-os"), plt.grid(b=True)
from numpy import*
M = array([[3,2,3],[1,2,6]])
class AriSred(object):
def __init__(self,m):
self.m=m
def srednja(self):
redovi = len(M)
stupci = len (M[0])
lista=[]
a=0
suma=0
while a<stupci:
for i in range (0,redovi):
suma=suma+ M[i,a]
lista.append(suma)
a=a+1
suma=0
b=array(lista)
b=b/redovi
return b
OBJ = AriSred(M)
sr = OBJ.srednja()