【问题标题】:Add a mean and 3*std to a scatter plot using matplotlib使用 matplotlib 向散点图添加平均值和 3*std
【发布时间】:2020-07-07 09:01:15
【问题描述】:

我有一个散点图,想为平均值、3*std+mean 和 3*std-mean 添加一条直线。我似乎有平均绘图但无法计算出标准!谢谢

import numpy as np  
import pandas as pd
import matplotlib.pyplot as plt 

    for element in df_na.loc[:, 'Ag_ppb':'Zr_ppb']:
        temp_df = df_na.loc[:, ['Date', element]].dropna()
        fig =plt.figure()
        plt.scatter(temp_df['Date'], temp_df[element],c='black',s=10)
        plt.plot(temp_df['Date'],[df_na[element].mean()]*len(x))
        plt.xlabel('Date')
        plt.xticks(rotation =90, fontsize=5)
        plt.ylabel(element)
        plt.show() 

【问题讨论】:

    标签: python matplotlib std mean


    【解决方案1】:

    你想使用 dataframe.std():

    df_na.std(axis=0,skipna=True)[element]
    

    【讨论】:

      【解决方案2】:

      所以我结合了上面的工作,见下文:

      plt.plot(temp_df['Date'],[temp_df[element].mean(axis=0,skipna=True)]*len(x), c='red',label='Mean')
      

      但以下不会绘制 3* std + mean 。

       plt.plot(temp_df['Date'],[temp_df[element].mean()]+[temp_df[element].std(axis=0,skipna=True)*3]*len(x),label='3xstd') 
      

      【讨论】:

        【解决方案3】:

        上述方法有效,但不将平均值添加到 3*std 不会绘制为一条线。

        【讨论】:

          猜你喜欢
          • 2015-06-29
          • 1970-01-01
          • 1970-01-01
          • 2019-05-06
          • 2021-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-20
          相关资源
          最近更新 更多