以 Y 轴为百分比绘制线图(使用 PercentFormatter)
Plot A Lineplot with Y-Axis as Percentage (Using PercentFormatter)
我正在使用以下嵌套字典制作线图:
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()
但是,我不需要默认的百分比值(如图所示)。我希望 1st value
是 starting percentage
,随后的值是 relative percentage
。例如,第一个情节以 200%
开始,我想要 0%
,情节以 170%,
结束,我想要 -something%
。
如有任何建议,我们将不胜感激。谢谢!
对代码进行微小更改的一种方法是使 y
中的值相对于第一个值。也就是说,保持一切原样并替换:
y=df[x]['weight'],
与:
y=[a-df[x]['weight'][0] for a in df[x]['weight']],
我正在使用以下嵌套字典制作线图:
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()
但是,我不需要默认的百分比值(如图所示)。我希望 1st value
是 starting percentage
,随后的值是 relative percentage
。例如,第一个情节以 200%
开始,我想要 0%
,情节以 170%,
结束,我想要 -something%
。
如有任何建议,我们将不胜感激。谢谢!
对代码进行微小更改的一种方法是使 y
中的值相对于第一个值。也就是说,保持一切原样并替换:
y=df[x]['weight'],
与:
y=[a-df[x]['weight'][0] for a in df[x]['weight']],