[英]histogram subplots with multiple axes
我有 4 組(即 bbs)的 3.csv 文件(即副本),每組有 2 列:時間(X 軸)和交互頻率(Y 軸 1)。 我還需要 plot 誤差條到我已經實現的第二個 y 軸。 由於它們具有相似的路徑,因此我通過 filename = 讀取它們,每個集合和副本都帶有 %s 。
現在我可以一個接一個地 plot ,但我想要實現的是:
為 plot 創建一個子圖,將每組的所有副本放入一個無花果中,以將它們一起保存在一個 jpg 中。 並且沒有 12 jpg。 數字
為每個復制品配備三種顏色的循環儀,以便在最終圖中區分它們,並為每個圖加上一個共同的圖例
並最終有一個共同的 y 軸范圍,因此結果在視覺上具有可比性,因為我比較了直方圖中的頻率。
到目前為止,這是我的代碼:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
bbss = [60,80,100,120]
replicas = ["1", "2", "3"]
colors = ['tab:blue', 'tab:orange', 'tab:green']
for bbs in bbss:
for i, replica in enumerate(replicas):
filename = "elliottIV_HA_IHSS/box4x4x4/bbs%s/bbpm2/NA/pH7.0/r%s/analysis/mdf/MIN_1:13.dat" % (bbs, replica)
data = pd.read_csv(filename, engine='python', delimiter="\s+|:", names=["time", "distance", "mol", "atom"], usecols=[0,1,3,4])
fig, ax1 = plt.subplots()
data["mol"] -= 1
data["mol"].plot.hist(figsize=(15, 5), rwidth=0.8, bins=range(1,int(bbs/2+2)), align="left", ax=ax1)
plt.suptitle('Replica-{}, {} building blocks:'.format(replica, bbs), fontsize=14)
plt.xticks(range(1,int(bbs/2+1)))
plt.xlabel("Molecule Nr.")
#plt.savefig('{}bbs.jpg'.format(bbs), bbox_inches='tight')
#plt.clf()
ax2 = ax1.twinx()
m = data.groupby("mol").mean()["distance"].values
s = data.groupby("mol").std()["distance"].values
i = data.groupby("mol").mean().index.values
ax2.errorbar(i, m, yerr=s, marker="o", fmt="none", color="tab:red")
ax2.set_ylabel("Distance [nm]")
fig.tight_layout()
plt.show()
dataframe 看起來像這樣:
time distance mol atom
0 0.0 0.368683 16 3
1 1.0 0.364314 16 1
2 2.0 0.358840 16 3
3 3.0 0.321033 16 3
4 4.0 0.361127 16 3
... ... ... ... ...
249995 249995.0 0.536088 13 3
249996 249996.0 0.508320 13 3
249997 249997.0 0.475273 13 3
249998 249998.0 0.559773 13 3
249999 249999.0 0.515042 7 11
y軸1上的頻率顯示每個“mol”出現的頻率(有30個)X顯然是有250k時間步長的時間,第二個y軸顯示每個“mol”的距離列的平均值mol”以及錯誤和st.dev
希望能澄清一點-謝謝!
為簡單起見,我創建了以下數據集:
data = pd.DataFrame([[0.0, 0.368683 , 16,3], [1.0, 0.364314 , 15, 1], [2.0 , 0.358840 , 16 , 3], [3.0 , 0.321033 , 17 , 3], [4.0 , 0.361127 ,17 , 3]],
columns=["time", "distance", "mol", "atom"])
Afterwarfs 我通過以下代碼通過以下代碼行創建了所需的三個圖:
bbss = [60,80,100,120]^M
replicas = ["1", "2", "3"]^M
colors = ['blue', 'orange', 'green']^M
for bbs in bbss:^M
fig, axes = plt.subplots(3,1)
plt.xticks(range(1,int(bbs/2+1)))
plt.xlabel("Molecule Nr.")
for i, replica in enumerate(replicas):
filename = "elliottIV_HA_IHSS/box4x4x4/bbs%s/bbpm2/NA/pH7.0/r%s/analysis/mdf/MIN_1:13.dat" % (bbs, replica)^M
# data = pd.read_csv(filename, engine='python', delimiter="\s+|:", names=["time", "distance", "mol", "atom"], usecols=[0,1,3,4])
data["mol"] -= 1
ax = axes[i]
data["mol"].plot.hist(figsize=(15, 5), rwidth=0.8, bins=range(1,int(bbs/2+2)), color=colors[i], align="left", ax=ax)
ax2 = ax.twinx()
m = data.groupby("mol").mean()["distance"].values
s = data.groupby("mol").std()["distance"].values
i = data.groupby("mol").mean().index.values
ax2.errorbar(i, m, yerr=s, marker="o", fmt="none", color="tab:red")
ax2.set_ylabel("Distance [nm]")
plt.suptitle('Replica-{}, {} building blocks:'.format(replica, bbs), fontsize=14)
fig.tight_layout()
plt.show()
請注意您的解決方案,您應該取消注釋讀取數據的行。 生成的 plot 如下所示:
我希望這是您正在尋找的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.