網站首頁 編程語言 正文
一、繪制帶趨勢線的散點圖
實現功能:
在散點圖上添加趨勢線(線性擬合線)反映兩個變量是正相關、負相關或者無相關關系。
實現代碼:
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:\數據雜壇\datasets\mpg_ggplot2.csv")
實現效果:
在散點圖上添加趨勢線(線性擬合線)反映兩個變量是正相關、負相關或者無相關關系。紅藍兩組數據分別繪制出最佳的線性擬合線。
二、繪制邊緣直方圖
實現功能:
python繪制邊緣直方圖,用于展示X和Y之間的關系、及X和Y的單變量分布情況,常用于數據探索分析。
實現代碼:
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:\數據雜壇\datasets\mpg_ggplot2.csv")
實現效果:
原文鏈接:https://blog.csdn.net/sinat_41858359/article/details/125392091
相關推薦
- 2023-05-08 Android開發多手指觸控事件處理_Android
- 2022-03-14 has been blocked by CORS policy: Response to prefl
- 2022-08-07 QT利用QProcess獲取計算機硬件信息_C 語言
- 2023-07-31 el-tree默認展開或折疊,全選或全不選
- 2022-10-14 composer -vvv 命令
- 2023-01-15 mvn?打包報錯:no?compiler?is?provided?in?this?environme
- 2022-05-22 Shell腳本一鍵安裝Nginx服務自定義Nginx版本_linux shell
- 2023-02-27 python定時任務timeloop庫用法實例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支