python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖
一、繪制帶趨勢線的散點圖
實現(xiàn)功能:
在散點圖上添加趨勢線(線性擬合線)反映兩個變量是正相關(guān)、負相關(guān)或者無相關(guān)關(guān)系。
實現(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")實現(xiàn)效果:

在散點圖上添加趨勢線(線性擬合線)反映兩個變量是正相關(guān)、負相關(guān)或者無相關(guān)關(guān)系。紅藍兩組數(shù)據(jù)分別繪制出最佳的線性擬合線。
二、繪制邊緣直方圖
實現(xiàn)功能:
python繪制邊緣直方圖,用于展示X和Y之間的關(guān)系、及X和Y的單變量分布情況,常用于數(shù)據(jù)探索分析。
實現(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")實現(xiàn)效果:

到此這篇關(guān)于python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖的文章就介紹到這了,更多相關(guān)python繪制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django項目創(chuàng)建到啟動詳解(最全最詳細)
這篇文章主要給大家介紹了關(guān)于Django項目創(chuàng)建到啟動的步驟,本文介紹的方法算是最全最詳細的一個項目,需要的朋友可以參考下2019-09-09
Python實現(xiàn)K-means聚類算法并可視化生成動圖步驟詳解
這篇文章主要介紹了Python實現(xiàn)K-means聚類算法并可視化生成動圖,本文分步驟給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05
Python操作SQLite數(shù)據(jù)庫的方法詳解【導(dǎo)入,創(chuàng)建,游標(biāo),增刪改查等】
這篇文章主要介紹了Python操作SQLite數(shù)據(jù)庫的方法,簡單說明了sqlite數(shù)據(jù)庫的相關(guān)概念,并結(jié)合實例形式較為詳細的分析了Python針對sqlite數(shù)據(jù)庫的導(dǎo)入,創(chuàng)建,游標(biāo),增刪改查等操作技巧,需要的朋友可以參考下2017-07-07
解決linux下使用python打開terminal時報錯的問題
這篇文章主要介紹了linux下使用python打開terminal時報錯,本文通過兩種場景分析給大家詳細講解,需要的朋友可以參考下2023-03-03

