我正在尝试创建测量值散点图,其中 x 标签是 WIFI channel 。默认情况下,matplotlib 的标签间距与其数值成比例。但是,我希望它们在散点图上均匀分布。这可能吗?
这基本上就是我的绘图代码当前的样子: - 其中 chanPoints 是频率列表,测量值是测量值列表。
plt.scatter(chanPoints,measurements)
plt.xlabel('Frequency (MHz)')
plt.ylabel('EVM (dB)')
plt.xticks(Tchan,rotation = 90)
plt.title('EVM for 5G Channels by Site')
plt.show()
最佳答案
Numpy
您可以使用 numpy 创建一个数组,将 chanPoints
中的唯一项映射到数字 0,1,2...
。然后,您可以为每个数字指定相应的标签。
import matplotlib.pyplot as plt
import numpy as np
chanPoints = [4980, 4920,4920,5500,4980,5500,4980, 5500, 4920]
measurements = [5,6,4,3,5,8,4,6,3]
unique, index = np.unique(chanPoints, return_inverse=True)
plt.scatter(index, measurements)
plt.xlabel('Frequency (MHz)')
plt.ylabel('EVM (dB)')
plt.xticks(range(len(unique)), unique)
plt.title('EVM for 5G Channels by Site')
plt.show()
Seaborn
如果您乐意使用seaborn,这可以节省大量手动工作。 Seaborn 专门用于绘制分类数据。 chanPoints
将被解释为 x 轴上的类别,并且它们之间具有相同的间距,如果您是例如使用swarmplot
。如果多个点重叠,它们会被绘制在彼此旁边,这可能是一个优点,因为它允许查看该 channel 的测量数量。
import matplotlib.pyplot as plt
import seaborn.apionly as sns
chanPoints = [4980, 4920,4920,5500,4980,5500,4980, 5500, 4920]
measurements = [5,6,4,3,5,8,4,6,3]
sns.swarmplot(chanPoints, measurements)
plt.xlabel('Frequency (MHz)')
plt.ylabel('EVM (dB)')
plt.title('EVM for 5G Channels by Site')
plt.show()
关于python-2.7 - 在 matplotlib 散点图中设置 xticks 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271754/