1. 本际云推荐 - 专业推荐VPS、服务器,IDC点评首页
  2. 云主机运维
  3. VPS运维

python数据可视化制做带均线系统的移动平均线和边缘柱形图

制做带均线系统的移动平均线

本文将为大家介绍如何使用Python进行数据可视化,制做带均线系统的移动平均线。在移动平均线上再加上均线系统(线性拟合线)反映2个变量是正相关、反比或者无关联性。具体实现方案如下:

python数据可视化制做带均线系统的移动平均线和边缘柱形图

  1. 在移动平均线上再加上均线系统(线性拟合线)反映2个变量是正相关、反比或者无关联性。
  2. 蓝红2组数据信息各自设计出最理想的线性拟合线。

以下为代码实现:

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:数据杂坛datasetsmpg_ggplot2.csv")

制做边缘柱形图

本文将继续为大家介绍如何使用Python进行数据可视化,制做边缘柱形图。边缘柱形图用于呈现X和Y内在联系、及X和Y的单变量分布规律,主要运用于数据探索分析。具体实现方案如下:

  1. 导入相关库。
  2. 定义绘图函数,包括读取数据、创建子图、画散点图和柱形图等。

以下为代码实现:

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 Histogramsn 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:数据杂坛datasetsmpg_ggplot2.csv")

综上所述,本文重要讲述了如何制做带均线系统的移动平均线和边缘柱形图,同时给出了详细的代码实现及解释说明。这些数据可视化技术具有极强的实际意义,需要的小伙伴可以学习下。

原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/5793.html