简体   繁体   English

如何将文本添加到seaborn distplot

[英]How to add text to seaborn distplot

I am trying to add some text ie name subplots alphabetically in seaborn distplot. 我正在尝试添加一些文本,即在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.text()这给我错误

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 rmsd_all看起来像这样

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循环中写入ax.text,而不是在sns.distplot中将其写入,并且它起作用了!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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