欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python可視化分析繪制散點(diǎn)圖和邊界氣泡圖

 更新時(shí)間:2022年06月23日 17:14:46   作者:不再依然07  
這篇文章主要介紹了python可視化分析繪制散點(diǎn)圖和邊界氣泡圖,python繪制散點(diǎn)圖,展現(xiàn)兩個(gè)變量間的關(guān)系,當(dāng)數(shù)據(jù)包含多組時(shí),使用不同顏色和形狀區(qū)分

一、繪制散點(diǎn)圖

實(shí)現(xiàn)功能:

python繪制散點(diǎn)圖,展現(xiàn)兩個(gè)變量間的關(guān)系,當(dāng)數(shù)據(jù)包含多組時(shí),使用不同顏色和形狀區(qū)分。

實(shí)現(xiàn)代碼:

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:\數(shù)據(jù)雜壇\datasets\midwest_filter.csv")

實(shí)現(xiàn)效果:

二、繪制邊界氣泡圖

實(shí)現(xiàn)功能:

氣泡圖是散點(diǎn)圖中的一種類型,可以展現(xiàn)三個(gè)數(shù)值變量之間的關(guān)系,之前的文章介紹過(guò)一般的散點(diǎn)圖都是反映兩個(gè)數(shù)值型變量的關(guān)系,所以如果還想通過(guò)散點(diǎn)圖添加第三個(gè)數(shù)值型變量的信息,一般可以使用氣泡圖。氣泡圖的實(shí)質(zhì)就是通過(guò)第三個(gè)數(shù)值型變量控制每個(gè)散點(diǎn)的大小,點(diǎn)越大,代表的第三維數(shù)值越高,反之亦然。而邊界氣泡圖則是在氣泡圖添加第四個(gè)類別型變量的信息,將一些重要的點(diǎn)選出來(lái)并連接。

實(shí)現(xiàn)代碼:

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函數(shù),圈出重點(diǎn)關(guān)注的點(diǎn)
? ? ? ? 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:\數(shù)據(jù)雜壇\datasets\midwest_filter.csv")

實(shí)現(xiàn)效果:

到此這篇關(guān)于python可視化分析繪制散點(diǎn)圖和邊界氣泡圖的文章就介紹到這了,更多相關(guān)python繪制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python rsa 加密解密

    python rsa 加密解密

    本篇文章主要介紹了python rsa加密解密 (編解碼,base64編解碼)的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-03-03
  • python和go語(yǔ)言的區(qū)別是什么

    python和go語(yǔ)言的區(qū)別是什么

    在本篇文章中小編給大家整理的是一篇關(guān)于go語(yǔ)言和python的區(qū)別點(diǎn),需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • Python增量循環(huán)刪除MySQL表數(shù)據(jù)的方法

    Python增量循環(huán)刪除MySQL表數(shù)據(jù)的方法

    這篇文章主要介紹了Python增量循環(huán)刪除MySQL表數(shù)據(jù)的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • 詳細(xì)探究Python中的字典容器

    詳細(xì)探究Python中的字典容器

    這篇文章主要介紹了Python中的字典容器,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下
    2015-04-04
  • python numpy數(shù)組復(fù)制使用實(shí)例解析

    python numpy數(shù)組復(fù)制使用實(shí)例解析

    這篇文章主要介紹了python numpy數(shù)組復(fù)制使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python?3.11.0下載安裝并使用help查看模塊信息的方法

    Python?3.11.0下載安裝并使用help查看模塊信息的方法

    本文給大家介紹Python?3.11.0下載安裝并使用help查看模塊信息的相關(guān)知識(shí),首先給大家講解了Python?3.11.0下載及安裝緊接著介紹了在命令行使用help查看模塊信息的方法,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • Python 居然可以在 Excel 中畫(huà)畫(huà)你知道嗎

    Python 居然可以在 Excel 中畫(huà)畫(huà)你知道嗎

    哈嘍,哈嘍~對(duì)于Excel大家想到的是不是各種圖表制作,今天我們來(lái)個(gè)不一樣的。十字繡大家都知道吧,今天咱們來(lái)玩?zhèn)€電子版的十字繡
    2022-02-02
  • 基于python中pygame模塊的Linux下安裝過(guò)程(詳解)

    基于python中pygame模塊的Linux下安裝過(guò)程(詳解)

    下面小編就為大家?guī)?lái)一篇基于python中pygame模塊的Linux下安裝過(guò)程(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • python數(shù)據(jù)分析近年比特幣價(jià)格漲幅趨勢(shì)分布

    python數(shù)據(jù)分析近年比特幣價(jià)格漲幅趨勢(shì)分布

    這篇文章主要為大家介紹了python分析近年來(lái)比特幣價(jià)格漲幅趨勢(shì)的數(shù)據(jù)分布,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • Python爬取豆瓣數(shù)據(jù)實(shí)現(xiàn)過(guò)程解析

    Python爬取豆瓣數(shù)據(jù)實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了Python爬取豆瓣數(shù)據(jù)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10

最新評(píng)論