Python画图篇

For Data Mining

Posted by Jiayue Cai on April 18, 2018

Last updated on 2019-7-23…

概率密度图

可见特征f1是要保留的,特征f20是要舍弃的

import matplotlib.pyplot as plt
import seaborn as sns

def plot_kde(train, test, col, values=True):
    fig,ax =plt.subplots(1,2,figsize=(15,5))
        
    sns.kdeplot(train[col][train['label']==0],color='g',ax=ax[0])
    sns.kdeplot(train[col][train['label']==1],color='r',ax=ax[0])
    sns.kdeplot(train[col][train['label']==-1],color='y',ax=ax[0])

    sns.kdeplot(train[col],color='y',ax=ax[1])
    sns.kdeplot(test[col],color='b',ax=ax[1])  
    plt.show()
    del train, col,test

for one_col in train.columns:
    plot_kde(train,test,one_col)

networkx社会网络图

import networkx as nx

#初始化

G=nx.Graph()

#添加点

G.add_node('a')#添加点a

G.add_node(1,1)#用坐标来添加点

#添加边

G.add_edge('x','y')#添加边,起点为x,终点为y

G.add_weight_edges_from([('x','y',1.0)]) #第三个输入量为权值

list = [[('a','b',5.0),('b','c',3.0),('a','c',1.0)]
G.add_weight_edges_from([(list)])

for index in node.index:   #遍历添加边

    G.add_edge(str(node.loc[index].values[0]),str(node.loc[index].values[1]))

#画图

import matplotlib.pyplot as plt
nx.draw(G,pos = nx.random_layout(G),node_color = 'r',edge_color = 'b',with_labels = True)
plt.show()

#保存

plt.savefig("data/links.jpg")

pyecharts地理地图

from pyecharts import Map, Page, Style

value = [1.3, 1.3, 1.3, 3.3, 1.3, 1.3, 1.3, 1.5, 1.5, 1.5, 1.5, 3.3, 3.5, 2.9]
attr = ["Pakistan","Afghanistan","Tajikistan","Bangladesh","Jordan", "Syria","Iraq","Nigeria","S.Sudan","Somalia","Uganda","MozamBique","Philippines","Colombia"]

chart = Map("级别分布", width=1200, height=600)
chart.add("", attr, value, maptype="world", is_visualmap=True,
          visual_range=[1,5], visual_range_color=['#d94e5d', '#eac763', '#ffffff'],
          is_map_symbol_show=False, visual_text_color='#000')

chart.render(path="data/级别分布.html")