本文为学习笔记,仅供自己复习回顾使用,若有侵权可删除
第十章matplotlib配置
代码层面
import matplotlib as mpl
# usage of package matplotlib
## method1 of setting attribution
mpl.rc("lines",linewidth=2,color="c",linestyle="--")
## method2 of setting attribution
line = {"linewidth":2,"color":"c","linestyle":"--"}
mpl.rc("lines",**line)
import matplotlib as mpl
# usage of package matplotlib
mpl.rcParams["lines.linewidth"]=2
mpl.rcParams["lines.color"]="c"
mpl.rcParams["lines.linestyle"]="--"
项目层面(修改配置文件)
matplotlib.matplotlib_fname() #查找配置文件位置
配置文件 matplotlibrc 主要包括以下配置要素。
- ( 1) lines: 设置线条属性,包括颜色、 线条风格、 线条宽度和标记风格等。
- ( 2) patch:填充 2D 空间的图形对象,包括多边形和圆。
- ( 3) font:字体类别、字体风格、字体粗细和字体大小等。
- ( 4) text:文本颜色、 LaTex 渲染文本等。
- ( 5) axes:坐标轴的背景颜色、坐标轴的边缘颜色、刻度线的大小、刻度标签的字体大小等。
- ( 6) xtick 和 ytick: x 轴和 y 轴的主次要刻度线的大小、宽度、刻度线颜色和刻度标签大小等。
- ( 7) grid:网格颜色、网格线条风格、网格线条宽度和网格透明度。
- ( 8) legend:图例的文本大小、阴影、图例线框风格等。
- ( 9) figure:画布标题大小、画布标题粗细、画布分辨率( dpi)、画布背景颜色和边缘颜色等。
- ( 10) savefig:保存画布图像的分辨率、背景颜色和边缘颜色等。
第十一章字体设置
文本、字体属性设置(3种方式)
改变配置文件 matplotlibrc 的字体属性值和文本属性值
#法一
plt.rcParams["lines.linewidth"] = 8.0
plt.rcParams["lines.linestyle"] = "--"
# font properties in change
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.serif"] = "New Century Schoolbook"
plt.rcParams["font.style"] = "normal"
plt.rcParams["font.variant"] = "small-caps"
plt.rcParams["font.weight"] = "black"
plt.rcParams["font.size"] = 12.0
# text properties in change
plt.rcParams["text.color"] = "blue"
#法二
plt.title("Line Chart",color="red",family="New Century Schoolbook",
style="normal",variant="small-caps",weight="black",size=18)
# Add text in string 'Text instance' to axis at location 'x', 'y', data
# coordinates
# font properties and text properties in change
plt.text(2.25,.8,"FONT",color="blue",
fontdict={"family":"New Century Schoolbook","style":"normal","variant":"small-caps","weight":"black","size":28})
手动添加字体
- ( 1) 下载需要的字体,例如 New Century Schoolbook Bold.ttf。
- ( 2) 将字体放在字体库 INSTALL/matplotlib/mpl-data/fonts/ttf 中,其中 INSTALL 是类似于 Linux
- 平台上的/usr/lib/python3.5/site-packages 和 Windows 平台上的 C:\Python35\Lib\site-packages。
- ( 3)调用 matplotlib 中的模块 font_manager,使用模块 font_manager 中的类 FontProperties(fname),
- 将参数 fname 设定为字体 New Century Schoolbook Bold.ttf 所在的路径 fname="INSTALL/matplotlib/
- mpl-data/fonts/ttf/New Century Schoolbook Bold.ttf "。
- (4) 具体语句是 newfont = matplotlib.font_manager.FontProperties("INSTALL/matplotlib/mpl-data/
- fonts/ttf/New Century Schoolbook Bold.ttf "),字体文件名称以属性内容中的名称为主。
- ( 5) 将实例 newfont 作为参数 fontproperties 分别放在显示文本内容的函数中,具体语句是 matplotlib.pyplot.xlabel("textContent",fontproperties=newfont) , matplotlib.pyplot.ylabel ("textContent",fontproperties=newfont)。
#展示有哪些可以选择
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(16,8))
ax = fig.add_subplot(111)
# viewing family options
families = ["serif","sans-serif","fantasy","monospace"]
ax.text(-1,1,"family",fontsize=18,horizontalalignment='center')
pi = [0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1]
for i,family in enumerate(families):
ax.text(-1,pi[i],family,family=family,horizontalalignment='center')
# viewing size options
sizes = ["xx-small","x-small","small","medium","large","x-large","xx-large"]
ax.text(-0.5,1,"size",fontsize=18,horizontalalignment='center')
for i,size in enumerate(sizes):
ax.text(-0.5,pi[i],size,size=size,horizontalalignment='center')
# viewing style options
styles = ["normal","italic","oblique"]
ax.text(0,1,"style",fontsize=18,horizontalalignment='center')
for i,style in enumerate(styles):
ax.text(0,pi[i],style,family="sans-serif",style=style,horizontalalignment='center')
# viewing variant options
variants = ["normal","small-caps"]
ax.text(0.5,1,"variant",fontsize=18,horizontalalignment='center')
for i,variant in enumerate(variants):
ax.text(0.5,pi[i],variant,family="serif",variant=variant,horizontalalignment='center')
# viewing weight options
weights = ["light","normal","semibold","bold","black"]
ax.text(1,1,"weight",fontsize=18,horizontalalignment='center')
for i,weight in enumerate(weights):
ax.text(1,pi[i],weight,weight=weight,horizontalalignment='center')
ax.axis([-1.5,1.5,0.1,1.1])
ax.set_xticks([])
ax.set_yticks([])
plt.show()
第十二章颜色设置
颜色映射表
最常用的颜色映射表有 autumn、 bone、 cool、 copper、 flag、 gray、 hot、 hsv、 jet、 pink、 prism、 spring、 summer、 winter。 其他颜色映射表基本上可以分为以下 3 类。
- sequential:同一颜色从低饱和度过渡到高饱和度的单色颜色映射表;
- diverging:颜色从中间的明亮颜色开始,然后过渡到两个不同颜色范围的方向上;
- qualitative:让不同种类的数据可以彼此之间轻易地区分出来。 另外,还有以下可以使用的颜色映射表: afmhot、 brg、 bwr、 coolwarm、 CMRmap、 gnuplot、 ocean、 rainbow、 seismic、 terrain 等。 根据 ColorBrewer( colorbrewer2.org) 的颜色界定,出现了以下 3 种颜色映射表类型。
- ColorBrewer Diverging: BrBG、 PiYG、 PRGn、 PuOr 等;
- ColorBrewer Sequential: Blues、 BuGn、 BuPu、 GnBu 等;
- ColorBrewer Qualitative: Accent、 Dark2、 Paired、 Set1 等。
全部内置的颜色映射表都可以通过增加后缀“_r” 的方式进行反转,例如“jet_r”就是“jet” 的反向循环颜色映射表
matplotlib.pyplot.imshow(X)
matplotlib.pyplot.set_cmap("hot")
matplotlib.pyplot.set_cmap("jet")
plt.subplot(131)
# colormap:jet
plt.scatter(a,b,np.sqrt(np.power(a,exponent)+np.power(b,exponent))*100,
c=np.random.rand(100),
cmap=mpl.cm.jet,
marker="o",
zorder=1)
plt.subplot(132)
plt.scatter(a,b,50,marker="o",zorder=10)
plt.subplot(133)
# colormap:BuPu
plt.scatter(a,b,50,
c=np.random.rand(100),
cmap=mpl.cm.BuPu,
marker="+",
zorder=100)
其他
设置可变数目的参数 谈到设置可变数目的参数,就要先谈谈可变数目的关键字参数。
def function_creation(name1,name2,**kwagrs):
a = name1+name2
b = name2*name2
c = [a,b,nam3,name4]
return c
在下面的函数中,参数*agrs 表示可变数目的非关键字参数,也就是说,可以直接将信息放在函 数列表的末尾,而不需要用具体的参数进行信息赋值处理
def function_creation(name1,name2,*agrs):
a = name1+name2
b = name2*name2
c = [a,b,nam3,name4]
return c