matplotlib(三)——标记、线条等设置。
标记
默认情况下,标记类型为“圆点”,除了matplotlib提供的其他类型,还可以自己定制,常用的例子
'o' :Circle
'x' :Cross
'+' :Plus sign
'P' :Filled plus sign
'D': Filled diamond
's' :Square
'^' :Triangel
散点图:在plt.scatter()中,用s来设置大小,c来设置颜色。
线图:plt.plot()函数中,使用marker来定义标记类型,color定义颜色,ms来定义大小。例如plt.plot(n,marker='x',color='black',ms=12)
还可以通过修改markeredgecolor,markeredgewidth,markerfacecolor来改变标记的样式。
线条
线条颜色:c 或者 color属性。
线条粗细:lw或者linewidth属性
虚线样式: ls 或者 linestyle属性。还可以设置虚线的cap style,有三种:butt,projecting,round。
Spines
在matplotlib中,Spines指的是绘图区域周围的线。包括上下左右四条。它们的样式同样可以设置。plt.gca()可以获取Spines对象。例如
ax = plt.gca()
ax.Spines['left'].set_linewidth(3)
ax.Spines['bottom'].set_color('darkblue')
ax.Spines['right'].set_visible(True)
ax.Spines['top'].set_visible(False)
字体
字体可以设置font size, fontstyle,font family,rotation等属性
图例
显示图例:plt.legend(loc="best"),loc用来设置图例的位置。
loc的取值:‘best’:0,
‘upper right’:1,
‘upper left’:2,
‘lower left’ :3 ,
‘lower right’ :4 ,
‘right’ :5
‘center left’:6,
‘center right:7,
‘lower center’:8,
‘upper center’ :9 ,
‘center’ :10
还可以将其值设置为坐标,例如plt.legend(loc=(0.5,0.5))