加标签
1.plt.legend
-plt.plot(lanel=)
-局部设置字体prop=font
2.文本注释(设置标签)
-plt.annotate(text,xy,xytext,arrowprops = {“”:2})
text:需要注释的文本,字符串
xy:需要注释的坐标
xytext:注释文本所加在的位置
arrowprops:设置箭头,字典形式```
plt.scatter(x,y)
1.柱状图:plt.bar(x,height)
2.条形图:plt.barh(y,whidth)
练习1
为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格
60
80
40
30
70
90
95
需求量
100
50
120
135
65
45
40
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]
```python
import matplotlib.pyplot as plt
p = [60,80,40,30,70,90,95]
s = [100,50,120,135,65,45,40]
plt.scatter(p,s)
plt.show()
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20200815172845735.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhb2ppbmdfYmFieQ==,size_16,color_FFFFFF,t_70
练习2
电影数据如下:
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
需求:
• 直观体现出不同电影近三天的票房的对比情况
```python
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,701,2200,362]
day_13 = [6888,502,2045,208]
day_14 = [8368,312,4497,120]
plt.figure(figsize=(14,8))
movies_po = range(len(movies_name))
width = 0.2
po_l = [i-0.2 for i in movies_po]
plt.bar(po_l,day_12,width=width,label="12号",color="r")
po_c = movies_po
plt.bar(po_c,day_13,width=width,label="13号",color="g")
po_r = [i+0.2 for i in movies_po]
plt.bar(po_r,day_14,width=width,label="14号",color="b")
plt.xticks(movies_po,movies_name,fontproperties=font)
plt.legend(prop=font)
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.xlabel("电影",fontproperties=font)
plt.ylabel("票房",fontproperties=font)
plt.title("2020年12日至14日电影票房",fontproperties=font)
plt.show()