简体   繁体   中英

How to add text to seaborn distplot

I am trying to add some text ie name subplots alphabetically in seaborn distplot.

Below is the snippet of my code.

import numpy as np
import matplotlib.pyplot as plt
import math, os, pdb
import seaborn as sns

def all_plots(rmsd_data, i, k):
    sns.distplot(rmsd_data, hist=False, kde=True, 
             bins=100, color=color_list[i],
             # hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 2},
             label=titles[i], ax=ax[k],
             ax.text(0.02, 0.95, "({})".format(figure_alphabet[i]), transform=ax.transAxes, fontsize=11, fontweight='bold', va='top')
)

f, ax = plt.subplots(3, sharex=True, figsize=(10,10))
color_list = ["red", "green", "darkblue"]
for i in range(3):
    filename1=open(somefile)
    rmsd_all= np.loadtxt(filename1, dtype=float)
    rmsd_all = rmsd_all[:,0:]
    k=0
    for j in replica:
        all_plots(rmsd_all[:,j], i, k)
        k=k+1

f.text(0.5, 0.05, 'RMSD (Å)', ha='center', fontsize=12)
f.text(0.05, 0.5, "probability Density", va='center', rotation='vertical', fontsize=16)
f.subplots_adjust(hspace=0.25)
plt.ion()
plt.show()
plt.savefig()
plt.pause(5.0)
plt.show()

I have tried ax.text() which gives me error

ax[i].text(0.02, 0.95, "({})".format(figure_alphabet[i]), transform=ax[i].transAxes, fontsize=11, fontweight='bold', va='top')
^
SyntaxError: positional argument follows keyword argument

rmsd_all looks like this

array([[1.        , 0.47835878, 0.47642503, 0.42507957, 0.49148079],
   [2.        , 0.61796997, 0.450252  , 0.3737451 , 0.53768188],
   [3.        , 0.67351597, 0.43173896, 0.6295222 , 0.54695088],
   [4.        , 0.52944587, 0.58706632, 0.5278477 , 0.55438694],
   [5.        , 0.55547007, 0.43153315, 0.54432041, 0.52586783]])

I want to do the alphabetical naming as show in the following plot. this image is just for example my plot will look different.

So I Have to answer my own question I guess.

So I had to wirte ax.text in the for loop instead of writing it in sns.distplot and it worked !!!!

for i in range(3):
    filename1=open(somefile)
    rmsd_all= np.loadtxt(filename1, dtype=float)
    rmsd_all = rmsd_all[:,0:]         # Modify rmsd_all[-5000:,0:] to skip initial 15000 frames which are not equilibrated.
    # pdb.set_trace()
    k=0
    for j in replica:
        all_plots(rmsd_all[:,j], i, k)
        ax[i].text(0.02, 0.95, "({})".format(figure_alphabet[i]), transform=ax[i].transAxes, fontsize=11, fontweight='bold', va='top')
        k=k+1

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