简体   繁体   English

使用 matplotlib 将平均值和 3*std 添加到散点图

[英]Add a mean and 3*std to a scatter plot using matplotlib

I've got a scatter plot and want add a straight line for mean, 3*std+mean and 3*std-mean.我有一个散点图,想为平均值、3*std+mean 和 3*std-mean 添加一条直线。 I seem to have the mean plotting but can't work out the std!我似乎有平均绘图,但无法计算出标准! Thanks谢谢

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

你想使用 dataframe.std():

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

So I incorporated the above which works, see below:所以我合并了上面的工作,见下文:

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

but the following won't plot the 3* std + 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*std 不会绘制为一条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM