Browse Source

Adjust x-axis ticks for large SFSs

tforest 11 months ago
parent
commit
46a2990a2d
1 changed files with 43 additions and 18 deletions
  1. 43 18
      swp2.py

+ 43 - 18
swp2.py View File

4
 import math
4
 import math
5
 from scipy.special import gammaln
5
 from scipy.special import gammaln
6
 from matplotlib.backends.backend_pdf import PdfPages
6
 from matplotlib.backends.backend_pdf import PdfPages
7
+from matplotlib.ticker import MaxNLocator
7
 
8
 
8
 def log_facto(k):
9
 def log_facto(k):
9
     k = int(k)
10
     k = int(k)
320
     plt.title(title)
321
     plt.title(title)
321
     plt.savefig(title+'_Breakpts_Likelihood_AIC.pdf')
322
     plt.savefig(title+'_Breakpts_Likelihood_AIC.pdf')
322
 
323
 
323
-def plot_test_theta(folder_path, mu, tgen, title = "Title", theta_scale = True, breaks_max = 5):
324
+def plot_test_theta(folder_path, mu, tgen, title = "Title", theta_scale = True, breaks_max = 12):
324
     """
325
     """
325
     Use theta values as is to do basic plots.
326
     Use theta values as is to do basic plots.
326
     """
327
     """
338
                 epochs[k] = thetas
339
                 epochs[k] = thetas
339
     print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(k)+"\n*******\n")
340
     print("\n*******\n"+title+"\n--------\n"+"mu="+str(mu)+"\ntgen="+str(tgen)+"\nbreaks="+str(k)+"\n*******\n")
340
     print(cpt, "theta file(s) have been scanned.")
341
     print(cpt, "theta file(s) have been scanned.")
341
-
342
     # intialize figure 1
342
     # intialize figure 1
343
     my_dpi = 300
343
     my_dpi = 300
344
-    plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
344
+    # plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
345
+    # multiple fig
346
+    fig, ax = plt.subplots(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
347
+    # Add some extra space for the second axis at the bottom
348
+    fig.subplots_adjust(bottom=0.15)
349
+    twin = ax.twiny()
350
+    # Offset the right spine of twin2
351
+    # twin.spines.right.set_position(("axes", 1.2))
352
+    plots = []
345
     for epoch, theta in epochs.items():
353
     for epoch, theta in epochs.items():
346
         groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
354
         groups = np.array(list(theta.values()), dtype=object)[:, 1].tolist()
347
         x = []
355
         x = []
352
             y += list(np.repeat(thetas[i], len(group)))
360
             y += list(np.repeat(thetas[i], len(group)))
353
             if epoch == 0:
361
             if epoch == 0:
354
                 N0 = y[0]
362
                 N0 = y[0]
363
+                # compute the proportion of information used at each bin of the SFS
364
+                sum_theta_i = 0
365
+                for i in range(2, len(y)+2):
366
+                    sum_theta_i+=y[i-2] / (i-1)
367
+                prop = []
368
+                for k in range(2, len(y)+2):
369
+                    prop.append(y[k-2] / (k - 1) / sum_theta_i)
370
+                prop = prop[::-1]
371
+                # print(prop, "\n", sum(prop))
372
+        # normalise to N0 (N0 of epoch1)
355
         for i in range(len(y)):
373
         for i in range(len(y)):
356
             y[i] = y[i]/N0
374
             y[i] = y[i]/N0
357
-        # compute the proportion of information used at each bin of the SFS
358
-        sum_theta_i = 0
359
-        for i in range(2, len(y)-1):
360
-            sum_theta_i+=y[i] / (i-1)
361
-        prop = []
362
-        for k in range(2, len(y)-1):
363
-            prop.append(y[k] / (k - 1) / sum_theta_i)
364
-        # plot 
365
-        plt.plot(x, y, 'o', linestyle="dotted", alpha=0.75, lw=2, label = str(epoch)+' brks')
366
-        plt.xlabel("# breaks")
367
-        plt.ylabel("theta")
368
-        plt.legend(loc='upper right')
369
-        plt.savefig(title+'_test'+str(k)+'.pdf')
375
+        # plot
376
+        #plt.plot(x, y, 'o', linestyle="dotted", alpha=0.75, lw=2, label = str(epoch)+' brks')
377
+        p, = ax.plot(x, y, 'o', linestyle="dotted", alpha=0.75, lw=2, label = str(epoch)+' brks')
378
+        # add plot to the list of all plots to superimpose
379
+        plots.append(p)
380
+    # virtual line to get the second x axis for proportions
381
+    p0, = twin.plot(prop, y, alpha = 0, label="Proportion")
382
+    # Move twinned axis ticks and label from top to bottom
383
+    twin.xaxis.set_ticks_position("bottom")
384
+    twin.xaxis.set_label_position("bottom")
385
+    # Offset the twin axis below the host
386
+    twin.spines["bottom"].set_position(("axes", -0.1))
387
+    #ax.legend(handles=[p0]+plots)
388
+    ax.set_xlabel("# breaks")
389
+    # Set the x-axis locator to reduce the number of ticks = 10
390
+    ax.xaxis.set_major_locator(MaxNLocator(nbins=10))
391
+    ax.set_ylabel("theta")
392
+    # twin.set_ylabel("Proportion")
393
+    plt.legend(handles=plots, loc='upper right')
394
+    plt.savefig(title+'_raw'+str(k)+'.pdf')
370
     # fig 2 & 3
395
     # fig 2 & 3
371
     plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
396
     plt.figure(figsize=(5000/my_dpi, 2800/my_dpi), dpi=my_dpi)
372
     for epoch, theta in epochs.items():
397
     for epoch, theta in epochs.items():
394
         plt.xlabel("# breaks")
419
         plt.xlabel("# breaks")
395
         plt.ylabel("theta")
420
         plt.ylabel("theta")
396
         plt.legend(loc='upper right')
421
         plt.legend(loc='upper right')
397
-        plt.savefig(title+'_test'+str(k)+'.pdf')
422
+        plt.savefig(title+'_plot2_'+str(k)+'.pdf')
398
         # Plotting (fig 3) which is the same but log scale for x
423
         # Plotting (fig 3) which is the same but log scale for x
399
         plt.plot(x_2, y, 'o', linestyle="dotted", alpha=0.75, lw=2, label = str(epoch)+' brks')
424
         plt.plot(x_2, y, 'o', linestyle="dotted", alpha=0.75, lw=2, label = str(epoch)+' brks')
400
         plt.xscale('log')
425
         plt.xscale('log')
401
         plt.xlabel("# breaks")
426
         plt.xlabel("# breaks")
402
         plt.ylabel("theta")
427
         plt.ylabel("theta")
403
         plt.legend(loc='upper right')
428
         plt.legend(loc='upper right')
404
-        plt.savefig(title+'_test'+str(k)+'_log.pdf')
429
+        plt.savefig(title+'_plot3_'+str(k)+'_log.pdf')
405
 
430
 
406
 def save_multi_image(filename):
431
 def save_multi_image(filename):
407
     pp = PdfPages(filename)
432
     pp = PdfPages(filename)