python-使用与原始数组相同的顺序绘制具有字符串数组的x轴,并且不在matplotlib中按字母顺序对其进行排序
作者:互联网
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()
这是我在JupyterNotebook中得到的输出:
解决方法:
由于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)
情节:
注意:有关更多定制图,请参考:pylab date demo
标签:matplotlib,jupyter-notebook,arrays,python,numpy 来源: https://codeday.me/bug/20191110/2013447.html