簡體   English   中英

如何將文本添加到seaborn distplot

[英]How to add text to seaborn distplot

我正在嘗試添加一些文本,即在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]])

我想按字母順序命名,如下圖所示。 例如,此圖像只是我的情節看起來會有所不同。

所以我必須回答我自己的問題。

因此,我不得不在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