You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
git-songhp/src/mycode.py

53 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# 进价与零售价
basePrice, salePrice = 49, 75
# 计算买num个时的批发价买的越多单价越低
def compute(num):
return salePrice * (1-0.01*num)
# numbers用来存储顾客购买数量
# earns用来存储商场的盈利情况
# totalConsumption用来存储顾客消费总金额
# saves用来存储顾客节省的总金额
numbers = list(range(1, 31))
earns = []
totalConsumption = []
saves = []
# 根据顾客购买数量计算三组数据
for num in numbers:
earns.append(round(num*(compute(num)-basePrice), 2))
totalConsumption.append(round(num*compute(num), 2))
saves.append(round(num*(salePrice-compute(num)), 2))
# 绘制商家盈利和顾客节省的折线图,系统自动分配线条颜色
plt.plot(numbers, earns, label='商家盈利')
plt.plot(numbers, totalConsumption, label='顾客总消费', ls='-.')
plt.plot(numbers, saves, label='顾客节省', lw=3)
# 设置坐标轴文本
plt.xlabel('顾客购买数量(件)', fontproperties='simhei')
plt.ylabel('金额(元)', fontproperties='simhei')
# 设置图形标题
plt.title('数量-金额关系图', fontproperties='stkaiti')
# 创建字体,设置图例
myfont = fm.FontProperties(fname=r'C:\Windows\Fonts\STKAITI.ttf')
plt.legend(prop=myfont)
# 计算并标记商家盈利最多的批发数量
maxEarn = max(earns)
bestNumber = numbers[earns.index(maxEarn)]
# 散点图在相应位置绘制一个红色五角星详见8.3节
plt.scatter([bestNumber], [maxEarn], marker='*', color='red', s=120)
# 使用文本标注标记该位置
plt.annotate(xy=(bestNumber, maxEarn), # 箭头终点
xytext=(bestNumber-1, maxEarn+200),# 箭头起点
s=str(maxEarn), # 显示的标注文本
arrowprops=dict(arrowstyle="->")) # 箭头样式
# 显示图形
plt.show()