簡體   English   中英

用圖表分組注釋散點圖

[英]Annotating scatter plot in groupby plot

我有一個這樣的df:

ID    Prcp   NDVI   Year
1     1.4    0.1    2000
1     2.3    0.4    2001
1     4.4    0.8    2002
1     0.4    0.1    2003
2     2.1    0.6    2000
2     1.2    0.4    2001
2     3.4    0.7    2002
2     2.8    0.5    2003

我想為每個唯一ID做一個PrcpNDVI的散點圖。 然后,我想為繪圖中的每個特定點添加Year的數據標簽。 我想這樣做:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

df=pd.read_csv(r'H:\my_file.csv')
with PdfPages(r'H:\path_to_out\out.pdf') as pdf:
        for i, group in df.groupby('ID'):
                plot = group.plot(x=['Prcp'], y=['NDVI'], title='NDVI_' + str(i), kind='scatter').get_figure()
                n=df.Year
                b=df.Prcp
                c=df.NDVI
                for i, txt in enumerate(n):
                    plt.annotate(txt, (b[i],c[i]), fontsize=2.5)
                plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
                ax = plt.gca()
                ax.set_xlabel('Precipitation')
                ax.set_ylabel('Mean NDVI')
                pdf.savefig(plot, bbox_inches='tight', pad_inches=0)  
                plt.close(plot)

但這不能正常工作。

要僅為1個繪圖執行此操作,使用如下df:

 ID    Prcp   NDVI   Year
    1     1.4    0.1    2000
    1     2.3    0.4    2001
    1     4.4    0.8    2002
    1     0.4    0.1    2003

我會這樣做:

a=df.Prcp
b=df.NDVI
n=df.Year
with PdfPages(r'H:\graph.pdf') as pdf: 
        plt.title('NDVI')
        plt.xlabel('Prcp')
        plt.ylabel('NDVI')
        plt.scatter(df.Prcp,df.NDVI, facecolors='none', s=20, edgecolors='b')
        for i, txt in enumerate(n):
            plt.annotate(txt, (a[i],b[i]), fontsize=2.5)
        axes=plt.gca()
        fig=plt.gcf()
        pdf.savefig(fig)
        plt.show()

編輯:

我用它實現了它:

def label_point(Prcp,Mean, Year, ax):
                   a = pd.concat({'Prcp': Prcp, 'NDVI': NDVI, 'Year': Year}, axis=1)
                   for i, point in a.iterrows():
                       ax.text(point['Prcp'], point['NDVI'], str(point['Year']))
               label_point(group.Prcp, group.NDVI, group.Year, ax)

我這樣做了:

df=pd.read_csv(r'E:\path.csv')
with PdfPages(r'E:\pth.pdf') as pdf:
        for i, group in first.groupby('ID'):
               fig, ax = plt.subplots()
               plot = group.plot(x=['Prcp'], y=['NDVI'], title='NDVI_' + str(i), kind='scatter', ax=ax).get_figure()
               def label_point(Prcp, NDVI, Year, ax):
                   a = pd.concat({'Prcp': Prcp, 'NDVI': NDVI, 'Year': Year}, axis=1)
                   for i, point in a.iterrows():
                       ax.text(point['Prcp'], point['NDVI'], str(point['Year']))
               label_point(group.Prcp, group.NDVI, group.Year, ax)
               ax = plt.gca()
               ax.set_xlabel('Precipitation')
               ax.set_ylabel('Mean NDVI')
               pdf.savefig(plot, bbox_inches='tight', pad_inches=0)  
               plt.close(plot)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM