003《Python数据分析、挖掘与可视化(第2版)》/例9-1.py
import numpy as np
import matplotlib.pyplot as plt

base_price, sale_price = 49, 75              # 进价与零售价
numbers = np.arange(1, 31)                   # 顾客可能的购买数量,限购30件
# 与每个购买数量对应的实际单价
real_price = sale_price * (1 - 0.01*numbers)
# 与每个购买数量对应的商场盈利情况
earns = np.round(numbers * (real_price-base_price), 2)
# 与每个购买数量对应的顾客总消费
total_consumption = np.round(numbers * real_price, 2)
# 与每个购买数量对应的顾客节省情况
saves = np.round(numbers * (sale_price-real_price), 2)
# 绘制商家盈利、顾客消费总金额、顾客节省金额的折线图,使用不同的属性以便区分
plt.plot(numbers, earns, label='商家盈利', lw=3, color='red')
plt.plot(numbers, total_consumption, ls='--',
         label='顾客总消费', color='#66ff33')
plt.plot(numbers, saves, label='顾客节省', ls='-.', color='#000088')
# 设置坐标轴标签文本
plt.xlabel('顾客购买数量(件)', fontproperties='simhei')
plt.ylabel('金额(元)', fontproperties='simhei')
plt.title('数量-金额关系图', fontproperties='STKAITI', fontsize=20)
plt.legend(prop='STKAITI')

# 计算并标记商家盈利最多的批发数量
maxEarn = earns.max()
bestNumber = numbers[earns==maxEarn][0]
# 散点图,在相应位置绘制一个红色五角星,显示在其他图形元素之上,距离人眼最近
plt.scatter(bestNumber, maxEarn, marker='*', color='purple', s=240, zorder=5)
# 使用annotate()函数在指定位置进行文本标注
plt.annotate(xy=(bestNumber,maxEarn),                # 箭头终点坐标
             xytext=(bestNumber-1,maxEarn+200),      # 箭头起点坐标
             text=str((bestNumber,maxEarn)),         # 显示的标注文本
             # 箭头样式,arrowstyle不能和headlength、width一起使用
             arrowprops=dict(arrowstyle='->'))
plt.show()