我正試圖建立一個小區,在那里我可以根據空氣中的化學物質來檢查鳥的死亡率。我所面臨的挑戰是如何將這些圖畫在彼此的上面。
下面是我寫的代碼。基本上給定了6種化學品,我有6個獨立的軸,在每個軸上繪制上述化學品與鳥類死亡率的關系,周數為X軸。
盡管圖表的源頭來自于一個資料框架(X軸并不不同),但Seaborn還是分別繪制了這兩個圖表。
我嘗試使用bird_chem_df.plot()和vanilla matplotlib來看看這是否僅限于Seaborn,但沒有收獲。
我很想知道到底是什么原因導致X軸不一致,盡管資料來自同一個來源,而且它們共享同一個自變數。
uj5u.com熱心網友回復:
柱狀圖刻度線的位置是0索引,線狀圖刻度線不是。
axs的多維陣列轉換為一維,以方便訪問和迭代。
'week_number',與線狀圖相同,所以兩個圖之間有相同數量的刻度線。因此,與其用x='week_number'繪制,不如通過指定x=ax.get_xticks()x=bird_chem_df.index(因為索引是一個RangeIndex,從0開始),然后將xtick標簽改為bird_chem_df.week_numberpython 3.8.11, pandas 1.3.2, matplotlib 3.4.3, seaborn 0.11.2import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
data = {'week_number': [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34], 'Benzene': [0. 62, 0.459, 0.542, 0.631, 0. 56, 0.61, 0.691, 1.109, 0. 524, 0.87, 0.896, 0。 665, 0.898, 0.862, 0.611, 0。 37], ' Ethylbenzene': [0.364, 0.204, 0. 372, 0.36, 0.225, 0.412, 0. 332, 0.659, 0.241, 1.7, np.nan, 1。 2, 7.35, 0.352, 0.267, 0.154], 'PM2。 5': [14.621, 12.561, 11.174, 18. 307, 11.285, 20.202, 16.111, 13. 057, 11.301, 12.214, 18.173, 21. 308, 14.009, 14.111, 9.805, 7. 818], 'Toluene': [1.339, 0.999, 1. 18, 2.019, 1.217, 1.797, 1。 478, 1.896, 1.552, 2.201, 1. 101, 1.416, 1.215, 1.598, 1.356, 0。 877], 'm,p-Xylene': [1.059, 0.842, 1. 186, 1.116, 0.693, 1.372, 0. 972, 2.103, 0.568, 1.783, 1. 736, 1.486, 1.983, 1.082, 0.909, 0。 354], 'o-Xylene'/span>: [0.525, 0.328, 0. 356, 0.409, 0.265, 0.396, 0。 32, 0.634, 0.266, 1.543, 0. 74, 2.0, 0.93, 0.374, 0.328, 0. 15], 'total_bird_deaths': [13, 14, 224, 87。120, 165, 224, 252, 303, 416, 580, 537, 592, 713, 442, 798] }
bird_chem_df = pd.DataFrame(data)
target_chemicals_chart = ['Toluene', 'o-Xylene', 'm, p-二甲苯', '乙苯', '苯', 'PM2. 5']
target_chemicals_display = {'Toluene': [0, 2.5], 'o-二甲苯'。[0, 2.5], 'm,p-二甲苯': [0, 2.5], 'Benzene'/span>: [0, 2.5], '乙基苯': [0, 8], 'PM2.5': [0, 25]}。
chemical_index = 0, 25]}。
# 設定繪圖2, 3, figsize=(15, 7)
# 將axs轉換成一個一維陣列。
axs = axs.ravel()
for ax in axs:
target_chemical = target_chemicals_chart[chemical_index]。
ax.set_title(f"{target_chemical} vs. bird mortality")
p1 = sns.barplot(ax=ax, x="week_number", y=target_chemical, data=bird_chem_df, ci=None, color='lightsteelblue')
ax.set_ylim(target_chemicals_display[target_chemical])
bird = ax.twinx()
# plot against the same tick values as the bar plot, with x=ax.get_xticks()
p2 = sns.lineeplot(ax=鳥, x=ax.get_xticks(), y="total_bird_deaths", data=bird_chem_df, color='red', marker='o')
ax.set_xlabel("Weeks since Spill")
ax.set_ylabel("化學品的平均結果 - ug/m3")
fig.tight_layout()
- 要繪制所有的化學品列 。
data = {'week_number' : [19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34], 'Benzene': [0. 62, 0.459, 0.542, 0.631, 0. 56, 0.61, 0.691, 1.109, 0. 524, 0.87, 0.896, 0。 665, 0.898, 0.862, 0.611, 0。 37], ' Ethylbenzene': [0.364, 0.204, 0. 372, 0.36, 0.225, 0.412, 0. 332, 0.659, 0.241, 1.7, np.nan, 1。 2, 7.35, 0.352, 0.267, 0.154], 'PM2。 5': [14.621, 12.561, 11.174, 18. 307, 11.285, 20.202, 16.111, 13. 057, 11.301, 12.214, 18.173, 21. 308, 14.009, 14.111, 9.805, 7. 818], 'Toluene': [1.339, 0.999, 1. 18, 2.019, 1.217, 1.797, 1。 478, 1.896, 1.552, 2.201, 1. 101, 1.416, 1.215, 1.598, 1.356, 0。 877], 'm,p-Xylene': [1.059, 0.842, 1. 186, 1.116, 0.693, 1.372, 0. 972, 2.103, 0.568, 1.783, 1. 736, 1.486, 1.983, 1.082, 0.909, 0。 354], 'o-Xylene'/span>: [0.525, 0.328, 0. 356, 0.409, 0.265, 0.396, 0。 32, 0.634, 0.266, 1.543, 0. 74, 2.0, 0.93, 0.374, 0.328, 0. 15], 'total_bird_deaths': [13, 14, 224, 87。120, 165, 224, 252, 303, 416, 580, 537, 592, 713, 442, 798] }
bird_chem_df = pd.DataFrame(data)
target_chemicals_display = {'Toluene': [0, 2.5], 'o-二甲苯': [0, 2.5], 'm,p-二甲苯': [0, 2.5], 'Benzene'/span>: [0, 2.5], '乙基苯': [0, 8], 'PM2.5': [0, 25]}。
target_chemicals_chart = ['Toluene'/span>, 'o-Xylene'/span>, 'm。 p-二甲苯', '乙苯', '苯', 'PM2. 5']
for target_chemicals in target_chemicals_display。
# 設定繪圖。
fig, axs = plt.subplots(2, 3, figsize=(15, 7)
axs = axs.ravel()
for ax in axs:
ax.set_title(f"{target_chemical} vs. bird mortality")
p1 = sns.barplot(ax=ax, x=bird_chem_df.index, y=target_chemical, data=bird_chem_df, ci=None, color='lightsteelblue')
ax.set_ylim(target_chemicals_display[target_chemical])
bird = ax.twinx()
p2 = sns.lineeplot(ax=bird, x=bird_chem_df.index, y="total_bird_deaths"/span>, data=bird_chem_df, color='red'/span>, marker='o'/span>)
# 設定X軸的刻度標簽為星期數。
ax.set_xticks(ax.get_xticks())
ax.set_xticklabels(bird_chem_df.week_number)
ax.set_xlabel("Weeks since Spill")
ax.set_ylabel("化學品的平均結果 - ug/m3")
fig.tight_layout()
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328940.html
標籤:


