欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python matplot画图

程序员文章站 2022-03-18 21:46:52
...

做个简单记录,免得下次重新找API

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号


#**************************************************************************************************************
# 直方图
# X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
# C,S = np.cos(X), np.sin(X)
# plt.plot(X, C, color='blue', linewidth=6.0, linestyle='-')
# plt.plot(X, S, color='red', linewidth=3.0, linestyle='--')
# plt.show()

# data = pd.read_csv('data.csv')
# data.x.plot(kind = 'hist', bins = 20, color = 'blue', edgecolor = 'black', density = True, label = '直方图')
# data.x.plot(kind = 'kde', color = 'red', label = '核密度图')
# plt.xlabel('x')
# plt.ylabel('核密度值')
# # 显示图例
# plt.legend()
# # 显示图形
# plt.show()

#**************************************************************************************88
# 画柱状图
# y = [11, 343, 0]
# x = ['数据全部失效', '数据无失效', '数据部分失效']
# plt.bar(x, y, width=0.3, color=(0, 0.4470, 0.7410))
# plt.xlabel('数据失效情况')
# plt.ylabel("数量")
# plt.title("285号数据样本各类操作变量数据残缺情况数目")
# for a, b in zip(x, y):
#     plt.text(a, b, '%.0f' % b, ha='center', va='bottom', fontsize=11)
# plt.show()

#*********************************************************************************************8
# 折线图
mse = [0.002494, 0.002411, 0.002301, 0.002243, 0.002257, 0.002289, 0.002204, 0.002322, 0.002382]
r2 = [0.879465, 0.883461, 0.88878, 0.8916, 0.890903, 0.889347, 0.893444, 0.88775, 0.884849]
s = [3.24000032,4.4600032,4.68000324,4.68000324,4.68000324,4.68000324,4.68000324,4.68000324,4.68000324,4.52000196,4.52000196,4.52000196,4.52000196,4.52000196]

# x = range(0, 14, 1)
x= [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
y = s
plt.plot(x, y, color='r', marker='o', markerfacecolor='blue')
# 设置数字标签
# for a, b in zip(x, y):
#     plt.text(a, b, b, ha='center', va='bottom', fontsize=10)
plt.xlabel('操作单位次数')
plt.ylabel('预测产品硫含量')
plt.title('预测产品硫含量随单位操作次数的变化')
plt.xticks(x,x[::1])
plt.show()