简体   繁体   中英

Unable to plot Pandas groupby data of a scatter plot and line plots

I'm trying to plot a groupedby series in Table1 as a scatter plot with groupedby dataframes in Table2 as line plots having them all in one chart. I wasn't successful plotting them together. However, I observed that Table2 "Days" attribute wasn't plotting all it's entire values hence, I have the vertical line plot to represent the line charts like below:

CHART1: 这个

I'm looking at the chart to be something like this:

CHART2:

在此处输入图片说明 Some of what I tried to do, was to unstack and set new index but still proved unsuccessful. Any advice or help with this will be appreciated. Thanks.

Code:

grouped = df.groupby('id')
fig,ax = plt.subplots(1,1,figsize=(8,6))    
for k,grp in grouped:
    grp.set_index('Days', inplace=True)
    ax = grp.plot.scatter(x='Days', y='Values',color="black")
    grp.plot(y=["curve1","curve2","curve3"], ax=ax, title="One \ 
    scatter and 3 line plots",alpha=0.5)
    ax.set_xlabel("Days", fontsize=15)
    ax.set_ylabel("Values", fontsize=15)
    fig.tight_layout()
    plt.autoscale(enable=True, axis='y')
plt.show()
plt.close()

TABLE1:

     id     Days    Values
0   101     17.0    3.764706
1   101     24.0    4.000000
2   101     38.0    1.142857
3   101     51.0    0.461538
12  102     31.0    4.258065
13  102     61.0    4.166667
14  102     92.0    4.709677
15  102     123.0   3.516129
16  102     153.0   3.233333
17  102     171.0   10.277778
18  102     202.0   3.193548
19  102     217.0   6.400000
20  102     235.0   1.000000
21  102     246.0   13.545455
22  102     270.0   0.791667
23  102     288.0   5.333333
24  103     31.0    3.096774
25  103     61.0    3.166667
26  103     92.0    3.354839
28  103     103.0   3.636364
29  103     114.0   2.727273
32  103     145.0   6.870968
33  103     175.0   4.200000
34  103     190.0   1.266667
35  103     210.0   4.550000
36  104     31.0    11.677419
37  104     61.0    12.066667
38  104     92.0    11.935484
39  104     123.0   12.354839
40  104     125.0   246.000000
... ... ... ...
455873  125     259.0   4.612903
455874  125     281.0   5.727273
455875  125     309.0   9.750000
455876  125     340.0   5.935484
455877  126     31.0    2.935484
455878  126     61.0    1.800000
455879  126     92.0    3.709677
455880  126     122.0   1.500000
455881  126     153.0   5.645161
455882  126     184.0   2.903226
455883  126     215.0   2.870968
455884  126     245.0   3.966667
455885  126     273.0   5.607143
455886  126     303.0   3.533333
455887  126     334.0   4.129032
455888  126     365.0   7.064516
455889  127     31.0    0.193548
455890  127     61.0    0.366667
455891  127     92.0    0.258065
455892  127     122.0   0.266667
455893  127     153.0   2.903226
455894  127     183.0   3.366667
455895  127     214.0   1.354839
455896  127     245.0   2.064516
455897  128     276.0   0.548387
455898  128     304.0   0.964286
455899  128     334.0   1.133333
455900  128     365.0   2.000000
455913  129     31.0    4.129032
455914  129     61.0    4.10000

TABLE2:

    id            curve1         curve2      curve3     Days
0   101         3.762914    3.730533    3.762762    1.000000
1   101         3.762908    3.730441    3.762756    1.003222
2   101         3.762903    3.730350    3.762749    1.006444
3   101         3.762897    3.730258    3.762743    1.009666
12  102         4.255960    4.218177    4.255781    1.038665
13  102         4.255953    4.218074    4.255774    1.041887
14  102         4.255946    4.217972    4.255767    1.045109
15  102         4.255940    4.217870    4.255760    1.048331
16  103         3.095224    3.067467    3.095093    1.051554
17  103         3.095220    3.067393    3.095088    1.054776
18  103         3.095215    3.067319    3.095082    1.057998
19  103         3.095210    3.067245    3.095077    1.061220
20  103         3.095205    3.067171    3.095072    1.064442
21  103         3.095201    3.067097    3.095067    1.067664
22  103         3.095196    3.067024    3.095062    1.070886
23  103         3.095191    3.066950    3.095057    1.074108
24  104         3.095186    3.066876    3.095052    1.077330
25  104         11.671414   11.564402   11.670904   1.080552
26  104         11.671396   11.564125   11.670885   1.083775
28  104         11.671360   11.563572   11.670846   1.090219
29  104         11.671342   11.563296   11.670826   1.093441
32  104         11.671289   11.562468   11.670768   1.103107
33  104         11.671271   11.562193   11.670749   1.106329
34  104         11.671253   11.561918   11.670729   1.109551
35  104         11.671235   11.561644   11.670710   1.112773
36  105         11.671217   11.561369   11.670691   1.115996
37  105          3.901147   3.864344    3.900970    1.119218
38  105          3.901141   3.864252    3.900964    1.122440
39  105          3.901135   3.864161    3.900957    1.125662
40  105          3.901129   3.864070    3.900951    1.128884
... ... ... ... ... ...
455873  126     25.476410   43.694862   29.152562   1469.867894
455874  126     25.476371   43.694860   29.152534   1469.871116
455875  126     31.540981   54.096432   36.092270   1469.874338
455876  126     31.540932   54.096429   36.092236   1469.877560
455877  127     31.540884   54.096426   36.092202   1469.880782
455878  127     31.540835   54.096423   36.092168   1469.884004
455879  127     31.540787   54.096420   36.092134   1469.887227
455880  127     470.699660  807.310367  538.622101  1469.890449
455881  127     470.698938  807.310323  538.621591  1469.893671
455882  127     470.698216  807.310280  538.621081  1469.896893
455883  127     470.697494  807.310236  538.620572  1469.900115
455884  127     470.696772  807.310192  538.620062  1469.903337
455885  127     32.606000   55.923891   37.311189   1469.906559
455886  127     32.605950   55.923888   37.311153   1469.909781
455887  127     32.605900   55.923885   37.311118   1469.913003
455888  127     32.605850   55.923882   37.311083   1469.916225
455889  128     32.605800   55.923879   37.311047   1469.919448
455890  128     35.066803   60.144961   40.127214   1469.922670
455891  128     35.066749   60.144958   40.127176   1469.925892
455892  128     35.066696   60.144954   40.127138   1469.929114
455893  128     35.066642   60.144951   40.127100   1469.932336
455894  128     35.066588   60.144948   40.127062   1469.935558
455895  128     64.137502   110.006494  73.393256   1469.938780
455896  128     64.137403   110.006488  73.393186   1469.942002
455897  128     64.137305   110.006482  73.393117   1469.945224
455898  128     64.137207   110.006476  73.393047   1469.948446
455899  128     64.137108   110.006470  73.392978   1469.951669
455900  128     10.082793   17.293797   11.537886   1469.954891
455913  129     36.850407   63.206268   42.168777   1469.996778
455914  129     36.850351   63.206264   42.168737   1470.000000

So it looks like you are getting plots for only 1 id value from your for loop. In your for loop you create a new image and then plot the data from that id value onto that figure and then show it at the end of your loop. As you iterate through that loop that figure gets overwritten basically. You can solve your issue by moving your figure declaration above your for loop and then moving plt.show() below your loop so that all the data gets put on 1 figure and shown after the loop is over.

Edit

I made some random data and plotted it using the bulk of your code and it works. When you include the figure declaration inside the loop it creates a large number of images. You may also see problems due to your naming and then renaming of 'ax' which may be increasing your number of plots. Also there is no reason to include plt.close() if are using plt.show().

import numpy as np
from matplotlib import pyplot as plt

x= np.arange(0,10,1)
y = x**2-3*x


fig,ax = plt.subplots(1,1,figsize=(8,6))    
for f in np.arange(0,10,1):
    ax.plot(x,y+f)
    ax.set_xlabel("Days", fontsize=15)
    ax.set_ylabel("Values", fontsize=15)
    fig.tight_layout()
    plt.autoscale(enable=True, axis='y')    

plt.show()

在此处输入图片说明

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