我是数据可视化的新手,有一个关于x-limit范围的简单问题。由于我有4个不同的图,我使用2x2网格,使用add_subplot方法。以下是我到目前为止所做的事情:
fig = plt.figure()
ax = fig.add_subplot(221, facecolor='lightgrey')
weeks = []
for weekNum in df.WeekNum:
weeks.append(weekNum)
maxNumber = max(weeks)
sixWeekList = list(range(maxNumber-6, maxNumber))
ax.set_title('Photo', fontsize = 14)
ax.set_xlabel('Week Number')
ax.set_xlim(sixWeekList)
ax.set_ylabel('Percentage')
ax.set_ylim([0,100])
fig.tight_layout()
我的目标是每次运行脚本时最近6周的情节。但是,当我运行上面的程序时,它显示以下错误,并且不允许我设置x轴的范围。但是,如果我把[30,35]放在ax.set_xlim()中,它会顺利运行,例如:ax.set_xlim([30,35])
**ValueError** Traceback (most recent call last)
<ipython-input-165-cbbb1eabca92> in <module>()
---> ax.set_xlim(tmp)
ValueError: too many values to unpack (expected 2)
任何其他解决方法?
答案 0 :(得分:1)
尝试直接使用max和min指定x限制,而不是列表,这就是它失败的原因。
例如:
fig = plt.figure()
ax = fig.add_subplot(221, facecolor='lightgrey')
weeks = []
for weekNum in df.WeekNum:
weeks.append(weekNum)
maxNumber = max(weeks)
sixWeekList = list(range(maxNumber-6, maxNumber))
ax.set_title('Photo', fontsize = 14)
ax.set_xlabel('Week Number')
#just pass max and min here
ax.set_xlim(maxNumber-6, maxNumber)
ax.set_ylabel('Percentage')
ax.set_ylim([0,100])