**常用的辅助元素包括坐标轴、标题、图例、网格、参考线、参考区域、注释文本和表格**
1.设置坐标轴的标签
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("2020080603011")
plt.show
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
plt.title("2020080603011")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show(
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
kinds = ['购物','人情来往','餐饮美食','通信物流','生活日用','交通出行','休闲娱乐','其他']
money_scale = [800 / 3000, 100 / 3000, 1000 / 3000, 200 / 3000, 300 / 3000, 200 / 3000, 200 / 3000, 200 / 3000]
dev_position = [0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]
plt.pie(money_scale,labels=kinds,autopct='%.1f%%',shadow=True,explode=dev_position,startangle=180)
#添加标题
plt.title('支付宝月账单报告')
#添加图例
plt.legend(kinds, loc='upper right', bbox_to_anchor=[1.3, 1.1])
plt.show()
5.显示网格
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x_speed = np.arange(10,210,10)
y_distance = np.array([0.5,2.0,4.4,7.9,12.3,17.7,24.1,31.5,39.9,49.2,59.5,70.8,83.1,96.4,110.7,126.0,142.2,159.4,177.6,196.8])
plt.title('2020080603011')
plt.scatter(x_speed,y_distance,s=50,alpha=0.9,linewidths=0.3)
#设置x轴的标签、刻度标签
plt.xlabel('速度(km/h)')
plt.ylabel('制动距离(m)')
plt.xticks(x_speed)
#显示网格
plt.grid(b=True, linewidth=0.7)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加参考线
plt.axvline(x=0, linestyle='-.')
plt.axhline(y=0, linestyle='-.')
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加参考线
plt.axvline(x=0, linestyle='-.')
plt.axhline(y=0, linestyle='-.')
#添加参考区域
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
men_means = (90.5, 89.5, 88.7, 88.5, 85.2, 86.6)
women_means = (92.7, 87.0, 90.5, 85.0, 89.5, 89.8)
ind = np.arange(len(men_means)) #每组柱形的x位置
width = 0.2 #各柱形的宽度
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind - width / 2 ,men_means, width, label='男生平均成绩')
ax.bar(ind + 0.2 ,women_means, width, label='女生平均成绩')
ax.set_title('高二各班男生、女生英语平均成绩')
ax.set_ylabel('分数')
ax.set_xticks(ind)
ax.set_xticklabels(['高二1班', '高二2班', '高二3班', '高二4班', '高二5班', '高二6班'])
#添加参考线
ax.axhline(88.5, ls='--', linewidth=1.0, label='全体平均成绩')
ax.legend(loc="lower right")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加参考线
plt.axvline(x=0, linestyle='-.')
plt.axhline(y=0, linestyle='-.')
#添加参考区域
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)
#添加指向型注释文本
plt.annotate("最小值",
xy=(-np.pi / 2, -1.0),
xytext=(-(np.pi / 2), -0.5),
arrowprops=dict(arrowstyle="fancy"))
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加参考线
plt.axvline(x=0, linestyle='-.')
plt.axhline(y=0, linestyle='-.')
#添加参考区域
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)
#添加指向型注释文本
plt.annotate("最小值",
xy=(-np.pi / 2, -1.0),
xytext=(-(np.pi / 2), -0.5),
arrowprops=dict(arrowstyle="fancy"))
#添加无指向型注释文本
plt.text(3.1, 0.10, "y=sin(x)", bbox=dict(alpha=0.2))
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(1, 8)
y = np.array([10770, 16780, 24440, 30920, 37670, 48200, 57270])
bar_rects = plt.bar(x, y, tick_label=["FY2013", "FY2014", "FY2015", "FY2016", "FY2017", "FY2018", "FY2019"], width=0.5)
#添加无指向型注释文本
def autolabel(rects):
"""在每个矩形条的上方附加一个文本标签,以显示其高度"""
for rect in rects:
height = rect.get_height() #获取每个矩形条的高度
plt.text(rect.get_x() + rect.get_width() / 2, height + 300,
s='{}'.format(height),
ha='center', va='bottom')
autolabel(bar_rects)
plt.ylabel('GMV(亿元)')
plt.title(2020080603011)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
y1, y2 = np.sin(x), np.cos(x)
plt.plot(x, y1, x, y2)
#添加图例
lines = plt.plot(x, y1, x, y2)
plt.legend(lines, ['正弦', '余弦'], shadow=True, fancybox=True)
#设置x轴和y轴的标签
plt.xlabel("x轴")
plt.ylabel("y轴")
#添加标题
plt.title("正弦曲线和余弦曲线")
#添加参考线
plt.axvline(x=0, linestyle='-.')
plt.axhline(y=0, linestyle='-.')
#添加参考区域
plt.axvspan(xmin=0.5, xmax=2.0, alpha=0.3)
plt.axhspan(ymin=0.5, ymax=1.0, alpha=0.3)
#添加指向型注释文本
plt.annotate("最小值",
xy=(-np.pi / 2, -1.0),
xytext=(-(np.pi / 2), -0.5),
arrowprops=dict(arrowstyle="fancy"))
#添加无指向型注释文本
plt.text(3.1, 0.10, "y=sin(x)", bbox=dict(alpha=0.2))
#添加表格
plt.table(cellText=[[6, 6, 6], [8, 8, 8]],
colWidths=[0.1] * 3,
rowLabels=['第1行', '第2行'],
colLabels=['第1列', '第2列', '第3列'], loc='lower right')
#显示网格
plt.grid(b=True, axis='y', linewidth=0.8)
#设置x轴的刻度范围和刻度标签
plt.xlim(x.min() * 1.5, x.max() * 1.5)
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$\pi/2$', r'$\pi$'])
plt.show()