python-2.7 - 在 matplotlib 散点图中设置 xticks 的位置

标签 python-2.7 matplotlib

我正在尝试创建测量值散点图,其中 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()

This is an example of my current output

最佳答案

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()

enter image description here

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()

enter image description here

关于python-2.7 - 在 matplotlib 散点图中设置 xticks 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271754/

相关文章:

python - 密谋返回空白图形对象

python - 如何在 python 中使用 alpha 和 beta 参数绘制 Gamma 分布

matplotlib - 如何更改 matplotlib 3D 图轴的颜色?

python - 计算文件python 2.7中的字符和行

python-2.7 - Flask Web 应用程序未响应 EC2 上的外部请求

python-2.7 - 使用 pywinauto 从文本字段获取值(value)

python - %matplotlib 返回无效语法错误

jquery - 使用ajax和django传递信息

python - 何时以及如何使用 Python 的 RLock

python - 运行时错误 : Invalid DISPLAY variable