数据分析202008第3节课Matplotlib作业

练习1

为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]

#第3节课Matplotlib作业01
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties 
plt.rcParams['font.sans-serif']=['SimHei'] 

plt.figure(figsize=(10,5)) #生成新的图片,figsize:图片大小,dpi:透明度

font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=6)

price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]

#现象无序,无法呈现变化
#目标列表进行排序
#price.sort(reverse=True) # 单纯的调序会破坏原有的一一映射关系
#sales.sort(reverse=True)

#用高阶list来进行封装改进
import numpy as np
lists_adjust=[]
zip_adjust= zip(price,sales)#zip方法在Python 2 和Python 3中的不同

#print(lists_adjust)#<zip object at 0x000001D3A30781C0>

for each in zip_adjust:
    #print(each)
    lists_adjust.append(list(each))
#print(lists_adjust)
    
lists_adjust = sorted(lists_adjust, key=lambda x: int(x[0]))
#print(lists_adjust)

#赋值新的列表x_n y_n
x_n=[]
y_n=[]
for list_adjust in lists_adjust:
    x_n.append(list_adjust[0])
    y_n.append(list_adjust[1])

print(x_n)
print(y_n)

plt.plot(x_n,y_n,'-r', label='A', linewidth=5.0) #绘制折线图


#plt.plot(price,sales,'-r', label='A', linewidth=5.0) #绘制折线图
#plt.xticks(price,x_price,fontproperties=font) # 
#现象1:x数轴最后的95元字体很小,分析原因可能是x数轴刻度不均匀,于是做如下调整
#解决方法1:定义个新列表作为刻度值
x_l = range(min(price)-10,100,5)
#x_price = ["{}元".format(x) for x in price] # 刻度值与刻度标签并没有匹配导致ValueError 
x_price = ["{}元".format(x) for x in x_l] 
print(x_price)
plt.xticks(x_l,x_price,fontproperties=font)
# 问题2,调整刻度后,报错ValueError 数值类型错


# 坐标点标识
for x_i,y_i in zip(x_n,y_n):
    plt.annotate(f"{y_i}",xy=(x_i,y_i),xytext=(x_i,y_i-1),weight="bold", color="b")

plt.annotate("135",xy=(30,135), xytext=(30+1.0, 135+20),arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="r"))#单个点的标注
    
plt.xlabel("价格")#设置x轴标签

plt.ylabel('销数量(单位:个)')#设置y轴标签

plt.title('销量随价格的变化趋势图')#设置图标题

plt.grid()#根据x轴和y轴的数值展示轴网格
plt.savefig('第3节课Matplotlib作业01.png')#保存图片
plt.show()

# 分析结论: 从图中不能看出,销售量随价格的呈现负相关,即价格越高销售量越小

结果:
在这里插入图片描述

练习2

电影数据如下:

movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"] 
day_12 = [2358,399,2358,362] 
day_13 = [12357,156,2045,168] 
day_14 = [15746,312,4497,319]

需求:
• 用直观体现出不同电影近三天的票房的对比情况

from matplotlib import pyplot as plt
plt.figure(figsize=(14,8))

plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)

movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"] 
day_12 = [2358,399,2358,362] 
day_13 = [12357,156,2045,168] 
day_14 = [15746,312,4497,319]

# 设置柱子宽度
iwidth = 0.6


# 蓝黄柱子宽度都为0.35 怎么使蓝色显示左边  黄色显示右边
# 位置左移width/3  位置-width/3   [0-width/3,1-width/3,2-width/3]
po_l = [i-iwidth/3 for i in list(range(len(movies_name)))]
plt.bar(po_l,day_12,width=iwidth/3,label="day_12",color="r")

po_c = [i for i in list(range(len(movies_name)))]
plt.bar(po_c,day_13,width=iwidth/3,label="day_13",color="g")

po_r = [i+iwidth/3 for i in list(range(len(movies_name)))]
plt.bar(po_r,day_14,width=iwidth/3,label="day_14",color="b")

# 设置刻度 为movies_name
plt.xticks(range(len(movies_name)),movies_name)

# 设置图例 
plt.legend()

# 数据标签
#auto_label(po_l,day_12) #name 'auto_label' is not defined
# 设置数据标签
def auto_label(x_po,y_po):
    for x_i,y_i in zip(x_po,y_po):
        plt.annotate(f"{(y_i)}",xy=(x_i,y_i),xytext=(x_i,y_i+50))
        
# 数据标签
auto_label(po_l,day_12)
auto_label(po_c,day_13)
auto_label(po_r,day_14)


plt.xticks(list(range(len(movies_name))),movies_name)

plt.show()


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Narutolxy/article/details/108014104