python matplotlib数据可视化
2017-12-26 16:14
961 查看
数据可视化:
一般如果数据分为几类,就要把数据分开,分别画。
运行结果如图:
不需要分开的方法:
注意这里的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进一步探索中...
一般如果数据分为几类,就要把数据分开,分别画。
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进一步探索中...
相关文章推荐
- 7057-1.Python数据可视化:利用matplotlib中的subplot进行图中图展示
- [置顶] Python数据可视化—matplotlib笔记
- python爬虫webdriver.Chrome 数据可视化简单案例matplotlib
- python数据可视化matplotlib的使用
- 教程 | 5种快速易用的Python Matplotlib数据可视化方法
- Python数据可视化利器Matplotlib,绘图入门篇,Pyplot介绍
- 基于Python的数据可视化 matplotlib seaborn pandas
- Python数据分析之可视化一matplotlib(常用方法)
- python的数据可视化库 matplotlib 和 pyecharts
- Python数据可视化图像库MatPlotLib基本图像操作
- 【转】Python数据可视化利器Matplotlib,colors系列,颜色的指定形式
- python的数据可视化库 matplotlib 和 pyecharts
- python中数据分析数据可视化作图matplotlib
- Python数据挖掘04---matplotlib数据可视化分析
- Python进阶(四十)-数据可视化の使用matplotlib进行绘图
- python数据分析之数据可视化matplotlib
- python—matplotlib数据可视化实例注解系列-----之plot图线型设置
- Python数据可视化matplotlib(一)—— 图表的基本元素
- 用Python获取股价数据,通过Matplotlib可视化
- Python数据可视化-matplotlib学习1