import pandas
import matplotlib.pyplot as plt
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
### python画图默认不能中文,如需必要则要加入上述两条语句
#折线图绘制
data = pandas.read_excel("123.xlsx")
# print(data) 类似于matlab plot画图操作
plt.plot(data['depth'],data['value']/max(data['value']))
plt.xticks(rotation=0) #旋转角度
plt.xlabel("Depth")
plt.ylabel("Dose")
plt.title("IDD")
plt.xlim(0,200) #设置x轴最大最小值
plt.ylim(0,1.02)
plt.show()
# 一张图绘制多组数据
x = list(range(20))
y1 = []
y2 = []
for i in x:
y1.append(i*2+5)
y2.append(3*i-4)
fig = plt.figure()
plt.plot(x, y1, c='red', label = "y1")
plt.plot(x, y2, c='blue', label = "y2")
plt.legend(loc = "best") # loc 定位位置 best显示最合适的位置 哪里不会print(help(**))
plt.show()
# 子图操作 add_subplot(4,1,x),添加四行一列子图 x第几个图
fig = plt.figure(figsize=(6,3)) #figsize设置整个图片的大小
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(np.random.randint(1,5,5),np.arange(5))
ax2.plot(np.arange(5)*2,np.arange(5))
plt.show()
# 条形图
from matplotlib.pyplot import MultipleLocator
#从pyplot导入MultipleLocator类,这个类用于设置刻度间隔
data = pandas.read_excel("123.xlsx","Sheet2")
# print(data)
num_cols = ["type1","type2","type3","type4","type5"]
norm_reviews = data[num_cols]
print(norm_reviews)
print(norm_reviews.shape)
bar_heights = norm_reviews.sum(axis=0) #bar高度
print(bar_heights)
bar_positions = np.arange(5) + 1.0 #bar的位置
fig,ax = plt.subplots()
x_major_locator=MultipleLocator(1)
#把x轴的刻度间隔设置为1,并存在变量里
# ax.bar(bar_positions, bar_heights, 0.5) #0.3 bar的宽度 纵向的bar
ax.barh(bar_positions, bar_heights, 0.5) #0.3 bar的宽度 横向的bar
plt.tick_params(axis='both',which='major',labelsize=14)
ax.xaxis.set_major_locator(x_major_locator)
# plt.xlim(0,5.5)
plt.show()
# 散点图
x = list(np.arange(15))
y = [2*i for i in x]
fig,ax = plt.subplots()
ax.scatter(x, y, c="red")
plt.show()
#详细的可以看python plot的官网
Python_机器学习_简单绘图
最新推荐文章于 2024-07-13 15:45:19 发布