坐标轴标签设置
面向过程
面向过程的方式 使用plt.xlabel()和plt.ylabel()根据就近原则分别设置x轴和y轴的标签.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
plt.subplot(221)
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.subplot(224)
plt.plot(x,y1)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
可以注意到这里的y轴标签出现了旋转,如果不想进行旋转可以配置标签的rotation属性,值为顺时针旋转的角度,不知如此,还可以使用fontsize配置字体大小,使用color配置字体颜色
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
plt.subplot(221)
plt.plot(x,y)
plt.xlabel('x', fontsize=14,color='blue',rotation=45)
plt.ylabel('y',rotation=0)
plt.subplot(224)
plt.plot(x,y1)
plt.xlabel('x', fontsize=14,color='blue',rotation=45)
plt.ylabel('y',rotation=0)
plt.show()
面向对象
面向对象就是使用画布来调用方法.set_xlabel()和.set_ylabel()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
axes1 =plt.subplot(221)
axes1.plot(x,y)
axes1.set_xlabel('x', fontsize=14,color='blue',rotation=45)
axes1.set_ylabel('y',rotation=0)
axes2=plt.subplot(224)
axes2.plot(x,y1)
axes2.set_xlabel('x', fontsize=14,color='blue',rotation=45)
axes2.set_ylabel('y',rotation=0)
plt.show()
画布标题设置
面向过程
面向过程的方式使用plt.title()根据就近原则进行设置,同样也可设置color,rotation,fontsize
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
axes1 =plt.subplot(221)
plt.plot(x,y)
plt.xlabel('x', fontsize=14,color='blue',rotation=45)
plt.ylabel('y',rotation=0)
plt.title('sin(x)',fontsize=16,color='red',rotation=45)
axes2=plt.subplot(224)
plt.plot(x,y1)
plt.xlabel('x', fontsize=14,color='blue',rotation=45)
plt.ylabel('y',rotation=0)
plt.title('cos(x)',fontsize=16,color='red',rotation=45)
plt.show()
面向对象
面向对象使用画布对象.set_title()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
axes1 =plt.subplot(221)
axes1.plot(x,y)
axes1.set_xlabel('x', fontsize=14,color='blue',rotation=45)
axes1.set_ylabel('y',rotation=0)
axes1.set_title('sin(x)',fontsize=16,color='red',rotation=45)
axes2=plt.subplot(224)
axes2.plot(x,y1)
axes2.set_xlabel('x', fontsize=14,color='blue',rotation=45)
axes2.set_ylabel('y',rotation=0)
axes2.set_title('cos(x)',fontsize=16,color='red',rotation=45)
plt.show()
图例设置
图例一般是用与如果在一张图中出现多图时进行标注使用的,图例设置的面向对象和面向过程的方式差不多,分别是plt.legend()和画布对象.legend()
label设置
label可以理解为每张图的标签,需要为其起一个名字
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
plt.legend()
plt.show()
注意这里改为ax.legend()效果相同
loc参数
loc是legend()中的参数,用于指定图例的位置,可设置为的参数如下,分别有字符串格式和数字的格式,默认为‘best’,也就是0,‘best’指的是找到整张图中空隙最大最合适的地方放置图例
• 0: ‘best’(自动选择最佳位置)
• 1: ‘upper right’
• 2: ‘upper left’
• 3: ‘lower left’
• 4: ‘lower right’
• 5: ‘right’
• 6: ‘center left’
• 7: ‘center right’
• 8: ‘lower center’
• 9: ‘upper center’
• 10: ‘center’
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
ax.legend(loc = 'lower right')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
ax.legend(loc = 4)
plt.show()
不止如此,loc还支持使用坐标的形式,支持列表或者元组的格式
[0,0] 左下角
[0,1] 右下角
[1,0] 左上角
[1,1] 右上角
但是实际的索引值可以取小数,甚至可以不再0-1区间内,使用时需要注意可能出现遮挡图表,或生成在了图片边界之外
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
ax.legend(loc = (1,1))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
ax.legend(loc = (1.1,1))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi*2, 100)
y = np.sin(x)
y1 = np.cos(x)
fig, ax = plt.subplots(1,1)
ax.plot(x,y,label = 'sin(x)')
ax.plot(x,y1,label = 'cos(x)')
ax.legend(loc = (0.5,0.5))
plt.show()