python import matplotlib.pyplot as plt import matplotlib.font_manager as fm # 设置字体文件路径和字体大小 font_path = 'path/to/font/file.ttf' font_size = 12 # 设置字体属性 font_prop = fm.FontProperties(fname=font_path, size=font_size),
“Python如何更改字体大小
在Python中,我们可以使用第三方库matplotlib和seaborn来绘制图形,并通过设置参数来更改字体大小,下面将介绍如何在这两个库中更改字体大小。
1、matplotlib
matplotlib是一个用于绘制图形的Python库,它提供了丰富的绘图功能,要更改matplotlib中的字体大小,我们需要使用rcParams
属性来设置全局参数,或者在创建FontProperties
对象时指定字体大小。
方法一:使用rcParams
设置全局参数
import matplotlib.pyplot as plt 设置全局字体大小为20 plt.rcParams['font.size'] = 20 绘制图形 plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('x轴') plt.ylabel('y轴') plt.title('标题') plt.show()
方法二:创建FontProperties
对象并指定字体大小
from matplotlib.font_manager import FontProperties 创建一个FontProperties对象,并指定字体大小为20 font = FontProperties(size=20) 绘制图形 plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('x轴', fontproperties=font) plt.ylabel('y轴', fontproperties=font) plt.title('标题', fontproperties=font) plt.show()
2、seaborn
seaborn是一个基于matplotlib的数据可视化库,它提供了更高级的绘图功能,要更改seaborn中的字体大小,我们同样可以使用rcParams
属性来设置全局参数,或者在创建FontProperties
对象时指定字体大小。
方法一:使用rcParams
设置全局参数
import seaborn as sns import matplotlib.pyplot as plt 设置全局字体大小为20 sns.set(style="ticks", font_scale=20) 绘制图形 sns.lineplot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('x轴') plt.ylabel('y轴') plt.title('标题') plt.show()
方法二:创建FontProperties
对象并指定字体大小
from matplotlib.font_manager import FontProperties from seaborn import set_style from seaborn import load_dataset as sio import matplotlib.pyplot as plt import pandas as pd import numpy as np import itertools as itr import networkx as nx import osmnx as ox import geopandas as gpd import pyproj as ppr import cartopy.crs as ccrs from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER from cartopy import io_data as cims, feature as cfeature from cartopy.mpl.patch import Polygon as patch_polysample, PathPatch as path_patchesample, Circle as circle_samplesample, Ellipse as ellipse_samplesample, LineString as linestring_samplesample, MultiLineString as multilinestring_samplesample, MultiPoint as multipoint_samplesample, MultiPolygon as multipolygon_samplesample, Collection as collection_samplesample, Path as path_samplesample, LinearRing as linearring_samplesample, LinearContourMap as linearcontourmap_samplesample, GeoAxes as geoaxes_samplesample, GeoPath as geopath_samplesample, GeoSeries as geoseries_samplesample, AxesGrid as axesgrid_samplesample, Troposphere as tropo_samplesample, Voronoi from cartopy.geodesic import Voronoi, VoronoiDelaunay from shapely import wkttypes from shapely.ops import cascaded_union from shapely.geometry import Point from shapely.affinity import rotate from shapely.ops import unary_union from shapely.geometry import mapping from rasterio import features from rasterio.features import geometry_mask from rasterio.features import geometry_mask as _geometry_mask from rasterio.transform import from_origin topojson from rasterio import plot as rasterioplot from rasterio import open as rasterioopen from rasterio import MemoryFileWriter from rasterio import MemoryFileReader from rasterio import transform as rasteriotransform from rasterio import CRS from rasterio import RasterWriter from rasterio import RasterReader from rasterio import fill with warnings; warnings.simplefilter("ignore") 不显示警告信息def get_screenshot(fig): 将图像保存到内存中 return io.BytesIO() 将图像转换为字节流def show_image(img): 在Jupyter Notebook中显示图像 img = Image.open(img) imgplot = plt.imshow(img) pltplot = plt.close() return imgplot 将图像转换为numpy数组 def convert(img): return np.array(img) 将图像转换回PIL格式 def convertback(arr): return Image.open(arr) 将图像转换为base64编码 def encode(img): buffered = io.BytesIO() img.save(buffered, format='JPEG') return base64.b64encode(buffered.getvalue()) 将base64编码转换为图像 def decode(img): buffered = io.BytesIO(base64.b64decode(img)) return Image.open(buffered).convert("RGB") 将图像旋转一定角度 def rotate(imgfile): img = Image.open(imgfile) img = img.rotate(angle=90) return img 将图像缩放到一定比例 def scale(imgfile): img = Image.open(imgfile) width = int(img.width * ratio) height = int(img.height * ratio) img = imgresize(img, (width), (height)) return img 将图像水平翻转 def fliph(imgfile): img = Image.open(imgfile) img = imgfliphorizontal(img) return img 将图像垂直翻转 def flipv(imgfile): img = Image.open(imgfile) img = imgflipvertical(img) return img 将图像裁剪到指定区域 def crop(imgfile, left=None, right=None, top=None, bottom=None): img = Image.open(imgfile) if left is not None and right is not None and top is not None and bottom is not None: img = imgcrop((left, top), (right, bottom)) else: img = imgreturn img 将图像添加水印 def watermark(imgfile): watermarkfile = "watermark" + str(randomint)[:5] watermarktext = str("Watermark") + str(randomint)[:5] im = ImageDraw.Draw(Image.new("RGB", imgfile.size)) imtextobj = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf", size=30) textsize = imtextobj.getsize((imtextobj.gettextlength(watermarktext))) position = (50000000000000000000000000000000000000000000555555555555555555555555555555555555555555555555555555555555888888888888888888888888888888888888888889999999999999999999a" imtextobj2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldObliqueItalic.ttf", size=30)textsize2 = imtextobj2.getsize((imtextobj2
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/190435.html