python matplotlib数据可视化

数据可视化:



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

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进一步探索中...



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值