我正在使用以下嵌套字典來制作線圖:
df = {'A':
{'weight': [200, 190, 188, 180, 170],
'days_since_gym': [0, 91, 174, 205, 279],
'days_since_fasting': 40},
'B':
{'weight': [181, 175, 172, 165, 150],
'days_since_gym': [43, 171, 241, 273, 300],
'days_since_fasting': 100}}
在制作線圖時,我希望將Y-Axis刻度作為percentage我正在使用的值PercentFormatter:
# set the plot size
fig, ax = plt.subplots(2, figsize=(10, 6))
for i, x in enumerate(df.keys()):
sns.lineplot(
x=df[x]['days_since_gym'],
y=df[x]['weight'],
marker="o",
ax=ax[i],
)
ax[i].axvline(df[x]['days_since_fasting'], color='k', linestyle='--', label='Fasting Starts')
ax[i].set_xlim(left=0, right=365)
# Percentage y-axis
ax[i].yaxis.set_major_formatter(mtick.PercentFormatter())
plt.xlabel('Days Since Joined Gym')
plt.ylabel('Relastive Weight')
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
plt.show()

但是,我不想要默認的百分比值(如圖所示)。我希望 the 1st valuewill be thestarting percentage并且隨后的值將是relative percentage. 例如,第一個圖以200%我想要的開始,我想要0%的圖以170%,我想要的作為結束-something%。
任何建議,將不勝感激。謝謝!
uj5u.com熱心網友回復:
對代碼進行微小更改的一種方法是使值y相對于第一個值。也就是說,保持一切原樣并替換:
y=df[x]['weight'],
和:
y=[a-df[x]['weight'][0] for a in df[x]['weight']],
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333452.html
標籤:Python 熊猫 matplotlib 阴谋 海生
