Either Numpy or Matplotlib is changing the order of my np.array and it's conflicting with my plot. It's causing the months to be out of order while the corresponding data to still be in the same order which is causing the plot to look weird:
Numpy或Matplotlib正在改变我的np.array的顺序,它与我的情节相冲突。这导致月份出现故障,而相应的数据仍然处于相同的顺序,导致情节看起来很奇怪:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
plt.plot(month, f)
plt.xlabel('Month')
plt.ylabel('Temperature')
plt.title('Average Monthly Temperature in Elizabeth City, NC')
plt.show()
This is what i get as output in JupyterNotebook:
这是我在JupyterNotebook中输出的内容:
2 个解决方案
#1
2
Since month
is a string array, plt.plot()
command is sorting it alphabetically. So, we have to use the xticks
and then plot it like below to get the strings in the same order as it were in the original array month
.
由于month是一个字符串数组,plt.plot()命令按字母顺序排序。所以,我们必须使用xticks,然后像下面一样绘制它,以获得与原始数组月份相同的顺序。
In [16]: f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
...: month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
...: plt.xticks(range(len(f)), month)
...: plt.plot(f)
Plot:
Note: For more customized plots refer: pylab date demo
注意:有关更多自定义图表,请参阅:pylab日期演示
#2
1
You will need to use MonthLocator
and set_major_locator
as shown here: formatting timeseries x-axis in pandas/matplotlib
您将需要使用MonthLocator和set_major_locator,如下所示:在pandas / matplotlib中格式化时间序列x轴
Here is my attempt:
这是我的尝试:
import matplotlib.pyplot as plt
import numpy as np
import datetime
f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
# New stuff:
from matplotlib.dates import MonthLocator, DateFormatter
dates = []
for month in range(1, 13):
dates.append(datetime.datetime(year=2018, month=month, day=1))
plt.plot(dates, f)
ax = plt.gca()
ax.set_xlim([dates[0], dates[-1]])
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))
plt.xlabel('Month')
plt.ylabel('Temperature')
plt.title('Average Monthly Temperature in Elizabeth City, NC')
plt.show()
#1
2
Since month
is a string array, plt.plot()
command is sorting it alphabetically. So, we have to use the xticks
and then plot it like below to get the strings in the same order as it were in the original array month
.
由于month是一个字符串数组,plt.plot()命令按字母顺序排序。所以,我们必须使用xticks,然后像下面一样绘制它,以获得与原始数组月份相同的顺序。
In [16]: f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
...: month = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
...: plt.xticks(range(len(f)), month)
...: plt.plot(f)
Plot:
Note: For more customized plots refer: pylab date demo
注意:有关更多自定义图表,请参阅:pylab日期演示
#2
1
You will need to use MonthLocator
and set_major_locator
as shown here: formatting timeseries x-axis in pandas/matplotlib
您将需要使用MonthLocator和set_major_locator,如下所示:在pandas / matplotlib中格式化时间序列x轴
Here is my attempt:
这是我的尝试:
import matplotlib.pyplot as plt
import numpy as np
import datetime
f = np.array([53, 56, 63, 72, 79, 86, 89, 88, 83, 74, 65, 56])
# New stuff:
from matplotlib.dates import MonthLocator, DateFormatter
dates = []
for month in range(1, 13):
dates.append(datetime.datetime(year=2018, month=month, day=1))
plt.plot(dates, f)
ax = plt.gca()
ax.set_xlim([dates[0], dates[-1]])
ax.xaxis.set_major_locator(MonthLocator())
ax.xaxis.set_major_formatter(DateFormatter('%b'))
plt.xlabel('Month')
plt.ylabel('Temperature')
plt.title('Average Monthly Temperature in Elizabeth City, NC')
plt.show()