网络编程
位置:首页>> 网络编程>> Python编程>> python可视化分析绘制带趋势线的散点图和边缘直方图

python可视化分析绘制带趋势线的散点图和边缘直方图

作者:不再依然07  发布时间:2023-08-16 04:10:10 

标签:python,绘制,散点图,边缘,直方图

一、绘制带趋势线的散点图

实现功能:

在散点图上添加趋势线(线性拟合线)反映两个变量是正相关、负相关或者无相关关系。

实现代码:

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可视化分析绘制带趋势线的散点图和边缘直方图

在散点图上添加趋势线(线性拟合线)反映两个变量是正相关、负相关或者无相关关系。红蓝两组数据分别绘制出最佳的线性拟合线。

二、绘制边缘直方图

实现功能:

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")

实现效果:

python可视化分析绘制带趋势线的散点图和边缘直方图

来源:https://blog.csdn.net/sinat_41858359/article/details/125392091

0
投稿

猜你喜欢

  • 几天前我在考虑使用 python 从 whatsapp 发送消息。和你们一样,我开始潜伏在互联网上寻找一些解决方案并找到了关于twilio.
  • 在使用SQL Server 的过程,中由于经常需要从多个不同地点将数据集中起来或向多个地点复制数据,所以数据的导出,导入是极为常见的操作.我
  • 1. 横排往下会影响阅读速度。如12345678的单排单列数字,肯定是竖排阅读快。但多行多列的整块信息,横排并不见得就比竖排慢,比如所有简体
  • IE历来被web标准的拥护者所诟病,而当FireFox横空出世以后,更多的网页制作者开始关注web标准设计。看着FireFox的市场占有率不
  • 本文实例为大家分享了python实现文件批量重命名的具体代码,供大家参考,具体内容如下代码:# -*- coding:utf-8 -*-im
  • 如果在子类中需要父类的构造方法就需要显式地调用父类的构造方法,或者不重写父类的构造方法。子类不重写 __init__,实例化子类时,会自动调
  • 什么是品牌的视觉传达品牌,这个熟悉而又陌生的名词,有时总会让人产生误解。品牌很广,广到一个意会颇深的战略发展理念;品牌很小,小到一个清晰可见
  • 本文实例讲述了JS Object.preventExtensions(),Object.seal()与Object.freeze()用法。分
  • WAP站点,这似乎是一个有点落伍的东西。在诞生之初,它很简陋,只能通过一个叫WML的标记语言来搭建没有任何美感的文字+链接页面。而今,绝大部
  • 发现一个有意思的现象,labelimg打开图片和xml标签时候,看不到标注好的框框,仔细查看了xml文件,没发现什么异常,后面试一下,才发现
  • 通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存
  • 前言问题:做requests请求时遇到如下报错:{“code”:“500&
  • 有时候让了解放双手,让电脑来帮我们自动发一些我们想要发的消息,挺省力的,比如说白天写好了演讲稿,晚上要在群里进行文字演讲,那么我们就可以用脚
  • 通过百度云API接口抽取得到产品评论的观点,也掠去了很多评论中无用的内容以及符号,为后续进行文本主题挖掘或者规则的提取提供基础。工具 1、百
  • quicktool/view.py文件修改视图函数index(),渲染一个home.html模板from django.shortcuts
  • 先 Create table 吧create table emp(id int not null primary key,name varc
  • zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下:class zipfile
  • 内容摘要合理使用渐变留白网格布局提高字体应用明确而有效的导航设计漂亮、有用的页脚介绍优秀设计和卓越设计之间的区别是比较小的。一般人可能无法解
  • 自动更新统计信息的基本算法是: · 如果表格是在 tempdb 数据库表的基数是小于 6,自动更新到表的每个六个修改。 · 如果表的基数是大
  • 在进行数据抓取时,经常会遇到IP被限制的情况,常见的解决方案是搭建 * 池,或购买IP代理的服务。除此之外,还有一个另外的方法就是使用家里
手机版 网络编程 asp之家 www.aspxhome.com