轴上的数据未按预期顺序 [英] Data on axis is not in expected order
问题描述
我正在尝试在图表上绘制一些数据,但是当我使用 data_list
进行绘制时,一切都很奇怪.
I'm trying to plot some data on a graph, but when I do if I use data_list
, it's all wonky.
import numpy as np
from numpy import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
data_list = [('January', 1645480), ('February', 1608476), ('March', 1557113), ('April', 1391652), ('May', 1090298), ('July', 1150535), ('August', 1125931), ('September', 1158741), ('October', 1305849), ('November', 1407438), ('December', 1501733)]
working_list = [('April', 1391652), ('August', 1125931), ('December', 1501733), ('February', 1608476), ('January', 1645480), ('July', 1150535), ('March', 1557113), ('May', 1090298), ('November', 1407438), ('October', 1305849), ('September', 1158741)]
#### GRAPHING
def create_graph(data):
x, y = zip(*data)
plt.plot(x,y)
axes = plt.gca() # Get the Current Axes
axes.get_yaxis().get_major_formatter().set_scientific(False) # Turn off scientific
# Show data on Y axis points
for i, j in zip(x,y):
plt.annotate(str(j),xy=(i,j))
plt.show()
def main():
## Graphing
create_graph(working_list)
if __name__ == "__main__":
main()
但是如果我使用working_list
,它是正确的!(按列表顺序排列.我只是想在 X 轴上显示从 1 月到 12 月的数据)
But if I use working_list
, it's correct! (It's in the order of the list. I'm simply trying to get the data to show from Jan - December on X-axis)
我已经盯着这个看太久了——每个列表中的数据看起来都完全一样,只有 data_list
首先有一月",到十二月......我是不知道为什么这会像它那样抛弃图表.我确实注意到 X 轴月份和 Y 轴上的数据是正确的——但线路连接器全部关闭,正如我预期的顺序......
I've been staring at this for too long -- the data looks to be the exact same in each list, only data_list
has "January" first, through December...I'm not sure why that would throw off the graph like it does. I did notice that the X-Axis months and the data on the Y axis are correct -- but the line connector is all off, as is the order I expected...
推荐答案
Matplotlib 当前(从 2.1 版开始)在轴上的类别顺序方面存在问题.它总是在绘图之前对类别进行排序,并且您没有机会更改该顺序.这有望在下一个版本中得到修复,但在那之前您需要坚持在轴上绘制数值.
Matplotlib currently (as of version 2.1) has a problem with the order of categories on the axes. It will always sort the categories prior to plotting and you have no chance of changing that order. This will hopefully be fixed for the next release, but until then you would need to stick to plotting numeric values on the axes.
在这种情况下,这意味着您将数据绘制在某个索引上,然后相应地设置刻度线.当然,您也可以使用 DateTimes,但这似乎有点矫枉过正,因为您已经有了可用月份的列表.
In this case this would mean you plot the data against some index and later set the ticklabes accordingly. Of course you could also use DateTimes, but that seems a bit overkill is you already have a list of the months available.
import numpy as np
import matplotlib.pyplot as plt
data_list = [('January', 1645480), ('February', 1608476), ('March', 1557113),
('April', 1391652), ('May', 1090298), ('July', 1150535),
('August', 1125931), ('September', 1158741), ('October', 1305849),
('November', 1407438), ('December', 1501733)]
#### GRAPHING
def create_graph(data):
months, y = zip(*data)
plt.plot(range(len(months)),y)
axes = plt.gca() # Get the Current Axes
axes.get_yaxis().get_major_formatter().set_scientific(False)
axes.set_xticks(range(len(months)))
axes.set_xticklabels(months, rotation=45, ha="right")
# Show data on Y axis points
for i, j in enumerate(y):
plt.annotate(str(j),xy=(i,j))
plt.show()
create_graph(data_list)
这篇关于轴上的数据未按预期顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!