您的位置:首页 > 编程语言 > Python开发

python matplotlib数据可视化

2017-12-26 16:14 961 查看
数据可视化:

一般如果数据分为几类,就要把数据分开,分别画。

import numpy as np
import matplotlib.pyplot as plt

# 随机生成数据
group = np.random.uniform(0,10,size=[100,2])
labels=np.tile('A',[100,1])
rand_num = np.random.randint(2,size=100)
find_0 = np.where(rand_num==0)
labels[find_0] = 'B'

color = ['r','k']

# 因为labels是二维,所以np.where返回一个元组(tuple)
idx = np.where(labels == 'A')
class_a = group[idx[0]]
plt.scatter(group[:,0],group[:,1],c='r', label='B')
plt.scatter(class_a[:,0], class_a[:,1], c='k', label='A')

# 坐标轴
plt.xlabel('x1')
plt.ylabel('x2')

plt.legend(loc='best')
plt.show()


运行结果如图:



不需要分开的方法:

import numpy as np
import matplotlib.pyplot as plt

group = np.random.uniform(0,10,size=[100,2])
labels=[]

# 此方法行不通
# labels.append(np.random.randint(1,3,size=100))

for i in range(100):
j = np.random.randint(1,3)
labels.append(j)

plt.scatter(group[:, 0], group[:, 1], 15.0*np.array(labels), 15.0*np.array(labels))
# ax.scatter(group[:,0], group[:, 1])
plt.show()


注意这里的label类型应该只能是list,array行不通。

且labels疑似只能用大于0的整数

____________________________________________________________________________

matplotlib  annotations绘制树形图:



import matplotlib.pyplot as plt

decision_node = dict(boxstyle="sawtooth", fc="0.8")
leaf_node = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")

def plot_node(node_text, center_pt, parent_pt, node_type, ax1):
ax1.annotate(node_text, xy=parent_pt, xycoords='axes fraction', xytext=center_pt, \
textcoords='axes fraction', va="center", ha="center",\
bbox=node_type, arrowprops=arrow_args)

def creat_plot():
fig = plt.figure(1, facecolor='white')
fig.clf()
ax1 = plt.subplot(111, frameon=False)
plot_node(U'decision', (0.5, 0.1), (0.1, 0.5), decision_node, ax1)
plot_node(U'leaf', (0.8, 0.1), (0.3, 0.8), leaf_node, ax1)
plt.show()

creat_plot()

emmm进一步探索中...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python