在不改变比例的情况下在pyplot轴上显示计算值

时间:2017-10-14 17:54:08

标签: python matplotlib graph axis

我正在尝试在python中重现增长图表。基础数据使用年龄,但我希望以2个月的间隔显示年龄(x)轴。这可以完全用轴/标签选项完成,还是需要在数据中添加计算列?

这是为了自学,所以解释比代码更受欢迎。

这就是我所拥有的:

# Import the WHO weight-for-age data from Excel
import pandas
import matplotlib.pyplot as plt
import numpy as np

WHO_WFA = pandas.read_excel("PercentilesData.xlsx", sheetname="WGT_WHO")

# ... slice the dataframe and plot it
# steps omitted for brevity

# Plot with data:
plt.plot(maleAge, maleP01, maleAge, maleP1, maleAge, maleP3, maleAge, maleP5, maleAge, maleP10, maleAge, maleP15, maleAge, maleP25, maleAge, maleP50, maleAge, maleP75, maleAge, maleP85, maleAge, maleP90, maleAge, maleP95, maleAge, maleP97, maleAge, maleP99, maleAge, maleP999)

# Set up the axes/labels
plt.title('Weight-for-Age:  male (WHO)')
plt.xlabel('Age  (days)')
plt.ylabel('Weight  (kg)')
plt.axis([0,1900,0,30])
plt.grid(True)

plt.show()

WHO Weight-for-age, male

相反,x轴应该以2个月的增量(2,4,6,8,10,12 ......到60)进行刻度。我仍然需要使用天数绘制的数据!因此,x轴值60应显示为2,120(天)显示为4(月)等。

感谢您的帮助!

更新1 : 只要调整x轴范围,将值除以月持续时间就会产生相同的曲线:

# month duration
mo = 30.4375
# divide age in days by month duration
plt.plot(maleAge/mo, maleP01, maleAge/mo, maleP1, maleAge/mo, maleP3, maleAge/mo, maleP5, maleAge/mo, maleP10, maleAge/mo, maleP15, maleAge/mo, maleP25, maleAge/mo, maleP50, maleAge/mo, maleP75, maleAge/mo, maleP85, maleAge/mo, maleP90, maleAge/mo, maleP95, maleAge/mo, maleP97, maleAge/mo, maleP99, maleAge/mo, maleP999)
# Adjust the maximum x value to 60 ...
plt.axis([0,60,0,30])

这会产生:

WHO WfA chart in months

最后一步是如何使用每12个月的主要滴答和每2个月的次要滴答来显示轴。

真正的好奇: 世卫组织增长图表中的月份长度标准化为30.4375天。数据从0到1856天(完成60.98个月)。对于每一天(0,1,...... 1856),有15个权重值,每个权重值对应不同的百分位数曲线,共计27,840个值。

原始数据可在以下网址公开:WHO data/weight-for-age for boys

1 个答案:

答案 0 :(得分:1)

假设一个月有30.4375天,您可以将天数除以30.4375并获得月数。

plt.plot(maleAge/30.4375, maleP01, maleAge/30.4375, maleP1, ...)

要定义特定的滴答间隔,您可以使用matplotlib.ticker.MultipleLocator,主要滴答的间隔为12,次要滴答的间隔为2。

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

#create some dataset
age = np.arange(0,1857) # in days
df = pd.DataFrame({"age" : age})
for i in range(10):
    df["maleP{}".format(i)] = (3.8-0.9*(1+2*i/10.))*np.sqrt(age)/4.2

# Plot with data:
plt.plot(df["age"]/30.4375, df[["maleP{}".format(i) for i in range(10)]])

# Set up the axes/labels
plt.title('Weight-for-Age:  male (WHO)')
plt.xlabel('Age  (months)')
plt.ylabel('Weight  (kg)')
plt.axis([0,1900/30.4375,0,30])
plt.grid(True)

plt.gca().xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(12))
plt.gca().xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(2))

plt.show()

enter image description here