简体   繁体   中英

Fix scale for x-axis matplotlib

I have a dataframe like this:

   Allotment  NDII Percent  NDII Value  NDVI Percent  NDVI Value
0    Arnston        0.0550   -0.199448           NaN         NaN
1    Arnston        0.0000   -0.198036           NaN         NaN
2    Arnston        0.0000   -0.196623           NaN         NaN
3    Arnston        0.0000   -0.195211           NaN         NaN
4    Arnston        0.0000   -0.193799           NaN         NaN
5    Arnston        0.0275   -0.192386           NaN         NaN
6    Arnston        0.0550   -0.190974           NaN         NaN
7    Arnston        0.0000   -0.189561           NaN         NaN
8    Arnston        0.0275   -0.188149           NaN         NaN
9    Arnston        0.1926   -0.186737           NaN         NaN
10      Anex        0.0275   -0.185324           NaN         NaN
11      Anex        0.0275   -0.183912           NaN         NaN
12      Anex        0.1376   -0.182499           NaN         NaN
13      Anex        0.0000   -0.181087           NaN         NaN
14      Anex        0.1100   -0.179675           NaN         NaN
15      Anex        0.0550   -0.178262           NaN         NaN
16      Anex        0.0000   -0.176850           NaN         NaN
17      Anex        0.0275   -0.175437           NaN         NaN
18      Anex        0.1100   -0.174025           NaN         NaN
0    Arnston           NaN         NaN        5.0338    0.000000
1    Arnston           NaN         NaN        1.0000    0.002235
2    Arnston           NaN         NaN        0.0200    0.004469
3    Arnston           NaN         NaN        0.1000    0.006704
4    Arnston           NaN         NaN        2.0000    0.008939
5    Arnston           NaN         NaN        1.0000    0.011173
6    Arnston           NaN         NaN        2.0000    0.013408
7    Arnston           NaN         NaN        0.0000    0.015643
8    Arnston           NaN         NaN        0.0000    0.017877
9    Arnston           NaN         NaN        0.0000    0.020112
10   Arnston           NaN         NaN        0.0000    0.022346
11      Anex           NaN         NaN        1.0000    0.024581
12      Anex           NaN         NaN        3.0000    0.026816
13      Anex           NaN         NaN        5.0000    0.029050
14      Anex           NaN         NaN        0.0000    0.031285
15      Anex           NaN         NaN        0.0000    0.033520
16      Anex           NaN         NaN        0.0000    0.035754
17      Anex           NaN         NaN        0.0000    0.037989
18      Anex           NaN         NaN        0.0000    0.040224
19      Anex           NaN         NaN        0.0000    0.042458
20      Anex           NaN         NaN        0.0000    0.044693
21      Anex           NaN         NaN        0.0000    0.046928
22      Anex           NaN         NaN        0.0000    0.049162
23      Anex           NaN         NaN        0.0000    0.051397
24      Anex           NaN         NaN        0.0000    0.053631
25      Anex           NaN         NaN        0.0000    0.055866
26      Anex           NaN         NaN        0.0000    0.058101

And I want to plot based on a groupby of each allotment the NDVI Value and NDII value on the x-axis against their respective NDVI Percent and NDII Percent on the y-axis .

I am doing this like this:

import pandas as pd
import matplotlib.pyplot as plt

with PdfPages(r'C:\Delete.pdf') as pdf:
    for i, group in df.groupby('Allotment'):
        Plot=group.plot(x=['NDVI Value','NDII Value'], y=['NDVI Percent','NDII Percent'], title=str(i)).get_figure()
        pdf.savefig(Plot)  

But the graphs come out like this:

Is there a way to set a standard x-axis ? Like if I know that all values fall between -0.5 and 1 I would like to set that as the label but I obviously need to make sure the graphed lines line up appropriately. Possibly the problem here is stemming from the NaN 's?

If there is a way to clean up the dataframe to remove those I am open to that as well.

If there is a way to create two subplots for each allotment that would work for me too

You can try set_xlim() (called AFTER the plot) This should force your x-axis to stay between 0.5 and 1 , and I believe it will scale it automatically.

set_xlim(0.5,1)

would go after you define Plot .

Best of luck, and happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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