如何向seaborn distp添加文本

2024-05-15 23:01:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试添加一些文本,即在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.文本()这给了我错误

^{pr2}$

rmsd都是这样的

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

我想按字母顺序命名,如下图所示。这张图片只是一个例子,我的情节会有所不同。


Tags: text文本import顺序as字母pltseaborn
1条回答
网友
1楼 · 发布于 2024-05-15 23:01:49

所以我得回答我自己的问题。在

所以我不得不ax.文本而不是在for循环中写入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

相关问题 更多 >