#12 matplotlab教学

安装

pip install matplotlib

折线图

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y轴为各月份最高温度
y =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
plt.plot(x, y) 
# 显示图表
plt.show()

长的像这样

点图

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y轴为各月份最高温度
y =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
plt.plot(x, y, "ob") 
# 显示图表
plt.show()

直方图

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y轴为各月份最高温度
y =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
plt.bar(x, y) 
# 显示图表
plt.show()

多条线

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y1为各月份平均温度
y1 =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# y2为各月份最高温度
y2 =  [23, 25, 26, 28, 29, 31, 30, 28, 26, 24, 22, 20]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
plt.plot(x, y1) 
# 第二条线
plt.plot(x, y2) 
# 显示图表
plt.show()

折线+点+直方+改颜色

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y1为各月份平均温度
y1 =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# y2为各月份最高温度
y2 =  [23, 25, 26, 28, 29, 31, 30, 28, 26, 24, 22, 20]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
plt.bar(x, y1, color = "gray") 
# 第二条线
plt.plot(x, y2, color = "red") 
plt.plot(x, y2, "o", color = "yellow" ) 
# 显示图表
plt.show()

图例

# 从matplotlib套件载入类别pyplot
from matplotlib import pyplot as plt 
 
# x轴为1~12月份
x = range(1,13) 
# y1为各月份平均温度
y1 =  [22, 21, 22, 25, 26, 28, 29, 31, 30, 29, 26, 24]
# y2为各月份最高温度
y2 =  [23, 25, 26, 28, 29, 31, 30, 28, 26, 24, 22, 20]
# 图表标题
plt.title("1 year temp") 
# x轴标题
plt.xlabel("month") 
# y轴标题
plt.ylabel("temp") 
# 绘制图表
l1 = plt.plot(x, y1, color = "gray", label = "AVG") 
# 第二条线
l2 = plt.plot(x, y2, color = "red", label = "MAX") 
#增加图例
plt.legend(loc='upper right')
# 显示图表
plt.show()


<<:  从零开始学3D游戏设计:基础粒子效果

>>:  (笔记D2) Spring MVC 框架处理流程

Day21. 伸缩自如的,向量图像炮 - SVG

昨天聊到小五郎叔叔脖子上的伤痕,今天要来聊日本的国民漫画航海王,大家有看过航海王的话想必对我们的主角...

连续 30 天 玩玩看 ProtoPie - Day 2

第二天,不废话,继续摸摸摸。 ProtoPie 的 Conceptual model 影片开始解释 ...

Day26 - AlertDialog

今天来练习第一个Dialog AlertDialog AlertDialog不仅仅提供使用者显示文字...

Flutter体验 Day 15-滚动组件

滚动组件 滚动组件是具有可滚动(Scroll)效果的内容区块,可以透过滚动的方式提供更多可浏览的内容...

[Day13] 以神经网络进行时间序列预测 — GRU

今天介绍使用 GRU 进行时间序列预测,一样采用我们最爱的股价资料集!! 今日大纲 GRU 介绍 门...