Python 绘图总结(Matplotlib 篇)之字体、⽂本及注释
学习 记录,描述不⼀定准确,具体请参考官⽹
Matplotlib使⽤总结图 字体
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
from matplotlib import font_manager as fm, rcParams
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fpath = os.path.join(rcParams["datapath"], "fonts/f")
prop = fm.FontProperties(fname=fpath)
fname = os.path.split(fpath)[1]
ax.set_title('This is a special font: {}'.format(fname), fontproperties=prop)
ax.set_xlabel('This is the default font')
plt.show()
显⽰⽂本
# 显⽰数学⽂本
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
<(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
<(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
plt.show()
import matplotlib.patches as patches
# build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
# axes coordinates are 0,0 is bottom left and 1,1 is upper right
p = patches.Rectangle(
(left, bottom), width, height,
fill=False, ansAxes, clip_on=False
)
ax.add_patch(p)
<(left, bottom, 'left top',
<(left, bottom, 'left top',
horizontalalignment='left',
verticalalignment='top',
ansAxes)
<(left, bottom, 'left bottom',
horizontalalignment='left',
verticalalignment='bottom',
ansAxes)
<(right, top, 'right bottom',
horizontalalignment='right',
verticalalignment='bottom',
ansAxes)
<(right, top, 'right top',
horizontalalignment='right',
verticalalignment='top',
ansAxes)
<(right, bottom, 'center top',
horizontalalignment='center',
verticalalignment='top',
ansAxes)
<(left, 0.5*(bottom+top), 'right center',
horizontalalignment='right',
verticalalignment='center',
rotation='vertical',
ansAxes)
<(left, 0.5*(bottom+top), 'left center',
horizontalalignment='left',
verticalalignment='center',
rotation='vertical',
ansAxes)
<(0.5*(left+right), 0.5*(bottom+top), 'middle', horizontalalignment='center',
verticalalignment='center',
fontsize=20, color='red',
ansAxes)
<(right, 0.5*(bottom+top), 'centered',
horizontalalignment='center',
verticalalignment='center',
rotation='vertical',
ansAxes)
<(left, top, 'rotated\nwith newlines',
horizontalalignment='center',
verticalalignment='center',
rotation=45,
ansAxes)
ax.set_axis_off()
plt.show()
⽔印
from __future__ import print_function
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.pyplot as plt
matplotlib中subplot# Fixing random state for reproducibility
np.random.seed(19680801)
datafile = _sample_data('logo2.png', asfileobj=False)
print('loading %s' % datafile)
im = image.imread(datafile)
im[:, :, -1] = 0.5# set the alpha channel
fig, ax = plt.subplots()
# ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7, mfc='orange')
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=0.7,mfc='orange')
# 添加⽔印
fig.figimage(im, 10, 10, zorder=3)
plt.show()
loading F:\Anaconda\lib\site-packages\matplotlib\mpl-data\sample_data\logo2.png
注释
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
<(3, 8, 'boxed italics text in data coords', style='italic',
bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
<(2, 6, r'an equation: $E=mc^2$', fontsize=15)
<(3, 2, u'unicode: Institut f\374r Festk\366rperphysik')
<(0.95, 0.01, 'colored text in axes coords',
verticalalignment='bottom', horizontalalignment='right',
ansAxes,
color='green', fontsize=15)
ax.plot([2], [1], 'o')
# 注释
ax.annotate('我是注释啦', xy=(2, 1), xytext=(3, 4),color='r',size=15, arrowprops=dict(facecolor='g', shrink=0.05))
ax.axis([0, 10, 0, 10])
plt.show()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论