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

python可視化分析繪制帶趨勢(shì)線的散點(diǎn)圖和邊緣直方圖

 更新時(shí)間:2022年06月24日 16:47:05   作者:不再依然07  
這篇文章主要介紹了python可視化分析繪制帶趨勢(shì)線的散點(diǎn)圖和邊緣直方圖,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

一、繪制帶趨勢(shì)線的散點(diǎn)圖

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

在散點(diǎn)圖上添加趨勢(shì)線(線性擬合線)反映兩個(gè)變量是正相關(guān)、負(fù)相關(guān)或者無(wú)相關(guān)關(guān)系。

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

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 Data
? ? df = pd.read_csv(file)
? ? df_select = df.loc[df.cyl.isin([4, 8]), :]

? ? # Plot
? ? gridobj = sns.lmplot(
? ? ? ? x="displ",
? ? ? ? y="hwy",
? ? ? ? hue="cyl",
? ? ? ? data=df_select,
? ? ? ? height=7,
? ? ? ? aspect=1.6,
? ? ? ? palette='Set1',
? ? ? ? scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))
? ? # Decorations
? ? sns.set(style="whitegrid", font_scale=1.5)
? ? gridobj.set(xlim=(0.5, 7.5), ylim=(10, 50))
? ? gridobj.fig.set_size_inches(10, 6)
? ? plt.tight_layout()
? ? plt.title("Scatterplot with line of best fit grouped by number of cylinders")
? ? plt.show()
draw_scatter("F:\數(shù)據(jù)雜壇\datasets\mpg_ggplot2.csv")

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

在散點(diǎn)圖上添加趨勢(shì)線(線性擬合線)反映兩個(gè)變量是正相關(guān)、負(fù)相關(guān)或者無(wú)相關(guān)關(guān)系。紅藍(lán)兩組數(shù)據(jù)分別繪制出最佳的線性擬合線。

二、繪制邊緣直方圖

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

python繪制邊緣直方圖,用于展示X和Y之間的關(guān)系、及X和Y的單變量分布情況,常用于數(shù)據(jù)探索分析。

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

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_Marginal_Histogram(file):
? ? # Import Data
? ? df = pd.read_csv(file)

? ? # Create Fig and gridspec
? ? fig = plt.figure(figsize=(10, 6), dpi=100)
? ? grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)
? ? # Define the axes
? ? ax_main = fig.add_subplot(grid[:-1, :-1])
? ? ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
? ? ax_bottom = fig.add_subplot(grid[-1, 0:-1], xticklabels=[], yticklabels=[])
? ? # Scatterplot on main ax
? ? ax_main.scatter('displ',
? ? ? ? ? ? ? ? ? ? 'hwy',
? ? ? ? ? ? ? ? ? ? s=df.cty * 4,
? ? ? ? ? ? ? ? ? ? c=df.manufacturer.astype('category').cat.codes,
? ? ? ? ? ? ? ? ? ? alpha=.9,
? ? ? ? ? ? ? ? ? ? data=df,
? ? ? ? ? ? ? ? ? ? cmap="Set1",
? ? ? ? ? ? ? ? ? ? edgecolors='gray',
? ? ? ? ? ? ? ? ? ? linewidths=.5)
? ? # histogram on the right
? ? ax_bottom.hist(df.displ,
? ? ? ? ? ? ? ? ? ?40,
? ? ? ? ? ? ? ? ? ?histtype='stepfilled',
? ? ? ? ? ? ? ? ? ?orientation='vertical',
? ? ? ? ? ? ? ? ? ?color='#098154')
? ? ax_bottom.invert_yaxis()
? ? # histogram in the bottom
? ? ax_right.hist(df.hwy,
? ? ? ? ? ? ? ? ? 40,
? ? ? ? ? ? ? ? ? histtype='stepfilled',
? ? ? ? ? ? ? ? ? orientation='horizontal',
? ? ? ? ? ? ? ? ? color='#098154')
? ? # Decorations
? ? ax_main.set(title='Scatterplot with Histograms \n displ vs hwy',
? ? ? ? ? ? ? ? xlabel='displ',
? ? ? ? ? ? ? ? ylabel='hwy')
? ? ax_main.title.set_fontsize(10)
? ? for item in ([ax_main.xaxis.label, ax_main.yaxis.label] +
? ? ? ? ? ? ? ? ?ax_main.get_xticklabels() + ax_main.get_yticklabels()):
? ? ? ? item.set_fontsize(10)

? ? xlabels = ax_main.get_xticks().tolist()
? ? ax_main.set_xticklabels(xlabels)
? ? plt.show()
draw_Marginal_Histogram("F:\數(shù)據(jù)雜壇\datasets\mpg_ggplot2.csv")

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

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

相關(guān)文章

  • 基于keras輸出中間層結(jié)果的2種實(shí)現(xiàn)方式

    基于keras輸出中間層結(jié)果的2種實(shí)現(xiàn)方式

    今天小編就為大家分享一篇基于keras輸出中間層結(jié)果的2種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • 利用Python讀取Excel表內(nèi)容的詳細(xì)過(guò)程

    利用Python讀取Excel表內(nèi)容的詳細(xì)過(guò)程

    python有多種方式可以去讀取excel文檔的內(nèi)容,下面這篇文章主要給大家介紹了利用Python讀取Excel表內(nèi)容的詳細(xì)過(guò)程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • 深入淺析Python字符編碼

    深入淺析Python字符編碼

    Python的字符串編碼規(guī)則一直讓我很頭疼,花了點(diǎn)時(shí)間研究了下,并不復(fù)雜,本文給大家介紹python字符編碼,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • 教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷

    教你如何使用Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)及三種遍歷

    什么是二叉樹(shù):每個(gè)節(jié)點(diǎn)最多有兩個(gè)子樹(shù)的樹(shù)結(jié)構(gòu),通常子樹(shù)被稱作“左子樹(shù)”(left subtree)和“右子樹(shù)”(right subtree) 二叉樹(shù)由兩個(gè)對(duì)象組成,一個(gè)是節(jié)點(diǎn)對(duì)象,一個(gè)是樹(shù)對(duì)象,需要的朋友可以參考下
    2021-06-06
  • python實(shí)現(xiàn)最大子序和(分治+動(dòng)態(tài)規(guī)劃)

    python實(shí)現(xiàn)最大子序和(分治+動(dòng)態(tài)規(guī)劃)

    這篇文章主要介紹了python實(shí)現(xiàn)最大子序和(分治+動(dòng)態(tài)規(guī)劃),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python實(shí)現(xiàn)while循環(huán)打印星星的四種形狀

    python實(shí)現(xiàn)while循環(huán)打印星星的四種形狀

    今天小編就為大家分享一篇python實(shí)現(xiàn)while循環(huán)打印星星的四種形狀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • django如何實(shí)現(xiàn)視圖重定向

    django如何實(shí)現(xiàn)視圖重定向

    這篇文章主要介紹了django如何實(shí)現(xiàn)視圖重定向,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 詳解Python中的變量及其命名和打印

    詳解Python中的變量及其命名和打印

    這篇文章主要介紹了Python中的變量及其命名和打印,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-03-03
  • python實(shí)現(xiàn)處理Excel表格超詳細(xì)系列

    python實(shí)現(xiàn)處理Excel表格超詳細(xì)系列

    這篇文章主要介紹了python實(shí)現(xiàn)處理Excel表格超詳細(xì)系列,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • 使用python腳本自動(dòng)生成K8S-YAML的方法示例

    使用python腳本自動(dòng)生成K8S-YAML的方法示例

    這篇文章主要介紹了使用python腳本自動(dòng)生成K8S-YAML的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論