python可視化分析繪制散點圖和邊界氣泡圖
一、繪制散點圖
實現功能:
python繪制散點圖,展現兩個變量間的關系,當數據包含多組時,使用不同顏色和形狀區分。
實現代碼:
import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings(action='once') plt.style.use('seaborn-whitegrid') sns.set_style("whitegrid") print(mpl.__version__) print(sns.__version__) def draw_scatter(file): # Import dataset midwest = pd.read_csv(file) # Prepare Data # Create as many colors as there are unique midwest['category'] categories = np.unique(midwest['category']) colors = [plt.cm.Set1(i / float(len(categories) - 1)) for i in range(len(categories))] # Draw Plot for Each Category plt.figure(figsize=(10, 6), dpi=100, facecolor='w', edgecolor='k') for i, category in enumerate(categories): plt.scatter('area', 'poptotal', data=midwest.loc[midwest.category == category, :],s=20,c=colors[i],label=str(category)) # Decorations plt.gca().set(xlim=(0.0, 0.1), ylim=(0, 90000),) plt.xticks(fontsize=10) plt.yticks(fontsize=10) plt.xlabel('Area', fontdict={'fontsize': 10}) plt.ylabel('Population', fontdict={'fontsize': 10}) plt.title("Scatterplot of Midwest Area vs Population", fontsize=12) plt.legend(fontsize=10) plt.show() draw_scatter("F:\數據雜壇\datasets\midwest_filter.csv")
實現效果:
二、繪制邊界氣泡圖
實現功能:
氣泡圖是散點圖中的一種類型,可以展現三個數值變量之間的關系,之前的文章介紹過一般的散點圖都是反映兩個數值型變量的關系,所以如果還想通過散點圖添加第三個數值型變量的信息,一般可以使用氣泡圖。氣泡圖的實質就是通過第三個數值型變量控制每個散點的大小,點越大,代表的第三維數值越高,反之亦然。而邊界氣泡圖則是在氣泡圖添加第四個類別型變量的信息,將一些重要的點選出來並連接。
實現代碼:
import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns import warnings from scipy.spatial import ConvexHull warnings.filterwarnings(action='once') plt.style.use('seaborn-whitegrid') sns.set_style("whitegrid") print(mpl.__version__) print(sns.__version__) def draw_scatter(file): # Step 1: Prepare Data midwest = pd.read_csv(file) # As many colors as there are unique midwest['category'] categories = np.unique(midwest['category']) colors = [plt.cm.Set1(i / float(len(categories) - 1)) for i in range(len(categories))] # Step 2: Draw Scatterplot with unique color for each category fig = plt.figure(figsize=(10, 6), dpi=80, facecolor='w', edgecolor='k') for i, category in enumerate(categories): plt.scatter('area','poptotal',data=midwest.loc[midwest.category == category, :],s='dot_size',c=colors[i],label=str(category),edgecolors='black',linewidths=.5) # Step 3: Encircling # https://stackoverflow.com/questions/44575681/how-do-i-encircle-different-data-sets-in-scatter-plot def encircle(x, y, ax=None, **kw): # 定義encircle函數,圈出重點關註的點 if not ax: ax = plt.gca() p = np.c_[x, y] hull = ConvexHull(p) poly = plt.Polygon(p[hull.vertices, :], **kw) ax.add_patch(poly) # Select data to be encircled midwest_encircle_data1 = midwest.loc[midwest.state == 'IN', :] encircle(midwest_encircle_data1.area,midwest_encircle_data1.poptotal,ec="pink",fc="#74C476",alpha=0.3) encircle(midwest_encircle_data1.area,midwest_encircle_data1.poptotal,ec="g",fc="none",linewidth=1.5) midwest_encircle_data6 = midwest.loc[midwest.state == 'WI', :] encircle(midwest_encircle_data6.area,midwest_encircle_data6.poptotal,ec="pink",fc="black",alpha=0.3) encircle(midwest_encircle_data6.area,midwest_encircle_data6.poptotal,ec="black",fc="none",linewidth=1.5,linestyle='--') # Step 4: Decorations plt.gca().set(xlim=(0.0, 0.1),ylim=(0, 90000),) plt.xticks(fontsize=12) plt.yticks(fontsize=12) plt.xlabel('Area', fontdict={'fontsize': 14}) plt.ylabel('Population', fontdict={'fontsize': 14}) plt.title("Bubble Plot with Encircling", fontsize=14) plt.legend(fontsize=10) plt.show() draw_scatter("F:\數據雜壇\datasets\midwest_filter.csv")
實現效果:
到此這篇關於python可視化分析繪制散點圖和邊界氣泡圖的文章就介紹到這瞭,更多相關python繪制內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖
- Python matplotlib繪制散點圖的實例代碼
- python數據分析之單因素分析線性擬合及地理編碼
- 一篇文章讓你快速掌握Pandas可視化圖表
- python 繪制斜率圖進行對比分析