目录
通用设置
import matplotlib.pyplot as plt
plt.figure(dpi=128, figsize=(10, 6))
plt.legend(labels = "y" ,loc="best")
'''
best 自动选择最佳位置,默认是左上
upper right 右上
upper left 左上
lower right 右下
lower left 左下
right 右边,默认右上。若右上被图形挡住,则自动往下选择空白地方绘制
center right 垂直居中且靠右
center left 垂直居中且靠左
lower center 垂直居中且靠底部
upper center 垂直居中且靠顶部
center 居中
'''
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
plt.xlim(0, 6)
plt.ylim(0, 30)
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
plt.tick_params(axis='both', labelsize=14)
plt.annotate("(2, 4)",
xy=(2, 4),
fontsize=16,
xycoords='data')
plt.text(2, 4, "this point very important",
fontdict={'size': 12, 'color': 'green'})
plt.show()
plt.savefig('squares_plot.png', bbox_inches='tight')
绘制折线图
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.figure(dpi=128, figsize=(10, 6))
plt.plot(x, y, linewidth=1, color="blue", linestyle="-")
plt.annotate("(2, 4)",
xy=(2, 4),
fontsize=16,
xycoords='data')
plt.text(2, 4, "this point very important",
fontdict={'size': 12, 'color': 'green'})
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
plt.legend(labels = "y" ,loc="best")
plt.xlim(0, 6)
plt.ylim(0, 30)
plt.tick_params(axis='both', labelsize=14)
plt.show()

绘制散点图
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.figure(dpi=128, figsize=(10, 6))
plt.scatter(x, y, c=y, edgecolor='none', s=10, cmap=plt.cm.Reds)
plt.show()

绘制柱状图
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.figure(dpi=128, figsize=(10, 6))
plt.bar(x, y, width=0.2, align="center", color="blue")
plt.show()
