Matplotlib画图

# coding=utf8

import numpy as np
#导入Matploylib库
from matplotlib import pyplot as plt
#在notebook中画图
#%matplotlib inline

# #折线图
# plt.plot([1,2,3],[4,5,1], label = "line1")
# plt.plot([1,2,3],[3,5,2], label = "line2")
#
# #图片的标题
# plt.title('Image Title')
# #坐标轴Y轴
# plt.ylabel('Y axis')
# #坐标轴X轴
# plt.xlabel('X axis')

# #柱状图
# plt.bar([0.25,1.25,2.25,3.25,4.25],[50,40,70,80,20],
#         label="BMW", color='b', width=.5)
# plt.bar([.75,1.75,2.75,3.75,4.75],[80,20,20,50,60],
#         label="Audi", color='r',width=.5)
# plt.xlabel('Days')
# plt.ylabel('Distance (kms)')


# #直方图  跟柱状图的区别是直方图显示的一个区间的数量
# population_age = [22,55,62,45,21,22,34,42,42,4,2,102,95,85,55,110,120,70,65,55,111,115,80,75,65,54,44,43,42,48]
# bins = [0,10,20,30,40,50,60,70,80,90,100]
# plt.hist(population_age, bins, histtype='bar', color='b', rwidth=0.8)


# #散点图
# x = [1,1.5,2,2.5,3,3.5,3.6]
# y = [7.5,8,8.5,9,9.5,10,10.5]
# x1=[8,8.5,9,9.5,10,10.5,11]
# y1=[3,3.5,3.7,4,4.5,5,5.2]
# plt.scatter(x,y, label='high income low saving',color='r')
# plt.scatter(x1,y1,label='low income high savings',color='b')
# plt.xlabel('saving*100')
# plt.ylabel('income*1000')
# plt.title('Scatter Plot')


# # 饼图 百分比图
# slices = [7,2,2,13]
# activities = ['sleeping','eating','working','playing']
# cols = ['c','m','r','b']
# plt.pie(slices,
#         labels=activities,
#         colors=cols,
#         startangle=90,
#         shadow= True,
#         explode=(0,0.1,0,0),
#         autopct='%1.1f%%')


# 多个图
# def f(t):
#     return np.exp(-t) * np.cos(2*np.pi*t)
# t1 = np.arange(0.0, 5.0, 0.1)
# t2 = np.arange(0.0, 5.0, 0.02)
# p1 = plt.subplot(221) #两行两列第1个
# plt.plot(t1, f(t1), 'bo', t2, f(t2))
# plt.setp(p1.get_xticklabels(), visible=False) #隐藏x轴标签文字
# plt.subplot(223, sharex=p1) #两行两列第3个 竖排列 sharex=p1代表x轴对齐
# plt.plot(t2, np.cos(2*np.pi*t2))

# 多个图和上面的区别是每个图是独立,相当于画了三个
def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(1.0, 5.0, 0.02)

p1,ax1 =plt.subplots(1) #两行两列第1个
plt.plot(t1, f(t1), 'bo', t2, f(t2))

p2,ax2 = plt.subplots(1) #两行两列第3个 竖排列
plt.plot(t2, np.cos(2*np.pi*t2))

#网格
plt.grid(True,color='k')
#在画布上显示
plt.legend()
plt.show()

转自 https://tianchi.aliyun.com/notebook/detail.html?spm=5176.11510288.0.0.11fcb7bdDwgzm3&id=7154

点线的形状颜色设置:https://blog.csdn.net/luanpeng825485697/article/details/78508819

此条目发表在未分类分类目录。将固定链接加入收藏夹。

发表评论