如何将文本添加到seaborn distplot

时间:2019-07-04 18:10:47

标签: python python-3.x seaborn

我正在尝试添加一些文本,即在seaborn distplot中按字母顺序命名子图。

下面是我的代码段。

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()

我尝试过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看起来像这样

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]])

我想按字母顺序命名,如下图所示。例如,此图像只是我的情节看起来会有所不同。

1 个答案:

答案 0 :(得分:0)

所以我必须回答我自己的问题。

所以我不得不在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