python中matplotlib的画图,三维图,柱状图,折线图

首先是三维图,直接上代码:

#这个是画三维图的


import numpy as np
import random
import xlrd
import xlwt

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x=np.linspace(-10,10,20)
y=np.linspace(-5,5,20)
z=np.linspace(-20,20,20)

print(x)
print(y)
print(z)

ax = plt.subplot(111, projection='3d')  # 创建一个三维的绘图工程
#  将数据点分成三部分画,在颜色上有区分度
ax.scatter(x[:10], y[:10], z[:10], c='r',marker='1')
ax.scatter(x[10:], y[10:], z[10:], c='g',marker='^')

ax.set_zlabel('Distance /km')  # 坐标轴
ax.set_ylabel('Num_L1')
ax.set_xlabel('Delay time /s')
plt.show()
效果如下:

python中matplotlib的画图,三维图,柱状图,折线图_第1张图片



接下来是柱状图,直接上代码:

#这个是画柱状图的

import numpy as np
import random
import xlrd
import xlwt

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x=[1,2,3,4,5,6,7,8,9,10]
y=np.linspace(0,10,10)

num_color=['r','saddlebrown','g','b','y','yellow','gold','orange','navy','indigo'] #颜色的集合
ax = plt.subplot(111)
ax.yaxis.grid(False, linestyle = "-.",color="silver")
ax.bar(x,y,1,color=num_color,tick_label=x)


for a,b in zip(x,y):   #在柱子上添加数据标签
    ax.text(a,b, '%.f'%b , ha='center', va= 'bottom',fontsize=7)
ax.set_ylim(0,20)
ax.set_title("accaracy under different k")
ax.set_xlabel("the value of k")
ax.set_ylabel("accuracy")
plt.show()

效果如下:

python中matplotlib的画图,三维图,柱状图,折线图_第2张图片


最后是折线图,代码如下:

import numpy as np
import random
import xlrd
import xlwt

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x=[1,2,3,4,5,6,7,8,9,10]
y=[3,4,5,6,7,8,9,10,11,12]

plt.grid(True,linestyle = "-.")

plt.plot(x,y,marker='*',color='green')

# 设置数字标签
for a, b in zip(x, y):
    plt.text(a, b+2, b, ha='center', va='bottom', fontsize=10)

plt.ylim(0,30)
plt.xlabel('the value of k')
plt.ylabel('number')
plt.title('red vs green')
plt.show()
 
  
效果如下:

python中matplotlib的画图,三维图,柱状图,折线图_第3张图片

你可能感兴趣的:(python)