【问题标题】:How can I shift overlapping data points slightly along the x-axis in matplotlib?如何在 matplotlib 中沿 x 轴稍微移动重叠数据点?
【发布时间】:2021-02-11 08:27:57
【问题描述】:

我为 4 种不同的实验设置收集了一些数据点,分别标记为“1”、“2”、“4”和“8”。对于每个实验设置,我收集了 10 个数据点。

我能够成功绘制这些数据点并绘制一条额外的曲线来显示每个设置的平均值。

但是,我在实验中更改了其他设置,并且可以绘制另一个类似的图形。现在我希望把所有东西都放在一个图中(2 组数据点和两条平均曲线),但是一切看起来都太拥挤了。我的绘图脚本和情节是这样的:

from numpy import *
import math
import matplotlib.pyplot as plt
import numpy as np

raw_1 = [0.38, 0.49, 0.25, 0.3, 0.4, 0.19, 0.45, 0.93, 0.44, 0.65] 
raw_2 = [0.27, 0.39, 0.09, 0.75, 0.79, 0.77, 0.31, 0.05, 0.73, 0.7]
raw_4 = [0.2, 0.84, 0.83, 0.7, 0.86, 0.2, 0.37, 0.41, 0.72, 0.29]
raw_8 = [0.2, 0.71, 0.31, 0.63, 0.24, 0.07, 0.2, 0.89, 0.34, 0.92]
y = np.array([raw_1, raw_2, raw_4, raw_8])
y = np.transpose(y)
y_mean = [mean(raw_1), mean(raw_2), mean(raw_4), mean(raw_8)]
x = [1,2,4,8]
xx = range(len(x))
plt.plot(xx, y[0], 'rx') 
plt.plot(xx, y[1], 'rx') 
plt.plot(xx, y[2], 'rx') 
plt.plot(xx, y[3], 'rx') 
plt.plot(xx, y[4], 'rx') 
plt.plot(xx, y[5], 'rx') 
plt.plot(xx, y[6], 'rx') 
plt.plot(xx, y[7], 'rx') 
plt.plot(xx, y[8], 'rx') 
plt.plot(xx, y[9], 'rx')

plt.xticks(xx,x)
leg = plt.legend(loc='upper left');

new_raw_1 = [0.217, 0.206, 0.222, 0.271, 0.212, 0.58, 0.333, 0.463, 0.314, 0.59] 
new_raw_2 = [0.511, 0.537, 0.565, 0.597, 0.527, 0.571, 0.505, 0.541, 0.542, 0.517]
new_raw_4 = [0.662, 0.552, 0.772, 0.436, 0.505, 0.577, 0.313, 0.796, 0.582, 0.574]
new_raw_8 = [0.511, 0.587, 0.591, 0.531, 0.522, 0.549, 0.593, 0.544, 0.552, 0.555]
y = np.array([new_raw_1, new_raw_2, new_raw_4, new_raw_8])
y = np.transpose(y)
y_mean_new = [mean(new_raw_1), mean(new_raw_2), mean(new_raw_4), mean(new_raw_8)]
x = [1,2,4,8]
xx = range(len(x))
plt.plot(xx, y[0], 'bo') 
plt.plot(xx, y[1], 'bo') 
plt.plot(xx, y[2], 'bo') 
plt.plot(xx, y[3], 'bo') 
plt.plot(xx, y[4], 'bo') 
plt.plot(xx, y[5], 'bo') 
plt.plot(xx, y[6], 'bo') 
plt.plot(xx, y[7], 'bo') 
plt.plot(xx, y[8], 'bo') 
plt.plot(xx, y[9], 'bo')
plt.plot(xx, y_mean_new, color='C0', marker='D', markersize=10, markerfacecolor='white', label='Avg A')
plt.plot(xx, y_mean, color='C1', marker='H', markersize=10, markerfacecolor='white', label='Avg B')
#plt.xticks(xx,x)
leg = plt.legend();


plt.show()

目前,蓝色圆圈和红色 X 标记相互遮挡。我应该如何修改我的脚本以在蓝色圆圈和红色 X 标记之间引入一个小的 x 轴偏移,同时仍然保持 xticks 为“1”、“2”、“4”、“8”等距离?

【问题讨论】:

  • 如果您有更多此类图表,您可能需要考虑使用seaborn - 它的 jitter 参数会自动处理此问题。

标签: python matplotlib data-visualization


【解决方案1】:

您只需为每个系列定义不同的 x 坐标列表,即可为 x 位置添加偏移量。这不会影响您的刻度,因为您只需为它们保留原始 x 位置。这是一个例子:

from numpy import *
import math
import matplotlib.pyplot as plt
import numpy as np

raw_1 = [0.38, 0.49, 0.25, 0.3, 0.4, 0.19, 0.45, 0.93, 0.44, 0.65] 
raw_2 = [0.27, 0.39, 0.09, 0.75, 0.79, 0.77, 0.31, 0.05, 0.73, 0.7]
raw_4 = [0.2, 0.84, 0.83, 0.7, 0.86, 0.2, 0.37, 0.41, 0.72, 0.29]
raw_8 = [0.2, 0.71, 0.31, 0.63, 0.24, 0.07, 0.2, 0.89, 0.34, 0.92]
y = np.array([raw_1, raw_2, raw_4, raw_8])
y = np.transpose(y)
y_mean = [mean(raw_1), mean(raw_2), mean(raw_4), mean(raw_8)]
x = [1,2,4,8]
xx = range(len(x))
xxr = [x - 0.1 for x in xx]
plt.plot(xxr, y[0], 'rx')
plt.plot(xxr, y[1], 'rx')
plt.plot(xxr, y[2], 'rx')
plt.plot(xxr, y[3], 'rx')
plt.plot(xxr, y[4], 'rx')
plt.plot(xxr, y[5], 'rx')
plt.plot(xxr, y[6], 'rx')
plt.plot(xxr, y[7], 'rx')
plt.plot(xxr, y[8], 'rx')
plt.plot(xxr, y[9], 'rx')

plt.xticks(xx,x)
leg = plt.legend(loc='upper left');

new_raw_1 = [0.217, 0.206, 0.222, 0.271, 0.212, 0.58, 0.333, 0.463, 0.314, 0.59] 
new_raw_2 = [0.511, 0.537, 0.565, 0.597, 0.527, 0.571, 0.505, 0.541, 0.542, 0.517]
new_raw_4 = [0.662, 0.552, 0.772, 0.436, 0.505, 0.577, 0.313, 0.796, 0.582, 0.574]
new_raw_8 = [0.511, 0.587, 0.591, 0.531, 0.522, 0.549, 0.593, 0.544, 0.552, 0.555]
y = np.array([new_raw_1, new_raw_2, new_raw_4, new_raw_8])
y = np.transpose(y)
y_mean_new = [mean(new_raw_1), mean(new_raw_2), mean(new_raw_4), mean(new_raw_8)]
x = [1,2,4,8]
xx = range(len(x))
xxb = [x + 0.1 for x in xx]
plt.plot(xxb, y[0], 'bo')
plt.plot(xxb, y[1], 'bo')
plt.plot(xxb, y[2], 'bo')
plt.plot(xxb, y[3], 'bo')
plt.plot(xxb, y[4], 'bo')
plt.plot(xxb, y[5], 'bo')
plt.plot(xxb, y[6], 'bo')
plt.plot(xxb, y[7], 'bo')
plt.plot(xxb, y[8], 'bo')
plt.plot(xxb, y[9], 'bo')
plt.plot(xx, y_mean_new, color='C0', marker='D', markersize=10, markerfacecolor='white', label='Avg A')
plt.plot(xx, y_mean, color='C1', marker='H', markersize=10, markerfacecolor='white', label='Avg B')
#plt.xticks(xx,x)
leg = plt.legend()

您还可以通过删除重复项(尤其是 numpy 的重复导入)和使用循环来避免重复自己来稍微改进代码:

我还在这个版本的推导中使用了唯一的变量名,因为我认为 x 的重用可能会在 Python 2.7 中导致问题。

import matplotlib.pyplot as plt
import numpy as np

raw_1 = [0.38, 0.49, 0.25, 0.3, 0.4, 0.19, 0.45, 0.93, 0.44, 0.65] 
raw_2 = [0.27, 0.39, 0.09, 0.75, 0.79, 0.77, 0.31, 0.05, 0.73, 0.7]
raw_4 = [0.2, 0.84, 0.83, 0.7, 0.86, 0.2, 0.37, 0.41, 0.72, 0.29]
raw_8 = [0.2, 0.71, 0.31, 0.63, 0.24, 0.07, 0.2, 0.89, 0.34, 0.92]
y = np.array([raw_1, raw_2, raw_4, raw_8])
y = np.transpose(y)
y_mean = [np.mean(raw_1), np.mean(raw_2), np.mean(raw_4), np.mean(raw_8)]
x = [1,2,4,8]
xx = range(len(x))

xxr = [j - 0.1 for j in xx]
for point in y:
    plt.plot(xxr, point, 'rx')

plt.xticks(xx,x)
leg = plt.legend(loc='upper left');

new_raw_1 = [0.217, 0.206, 0.222, 0.271, 0.212, 0.58, 0.333, 0.463, 0.314, 0.59] 
new_raw_2 = [0.511, 0.537, 0.565, 0.597, 0.527, 0.571, 0.505, 0.541, 0.542, 0.517]
new_raw_4 = [0.662, 0.552, 0.772, 0.436, 0.505, 0.577, 0.313, 0.796, 0.582, 0.574]
new_raw_8 = [0.511, 0.587, 0.591, 0.531, 0.522, 0.549, 0.593, 0.544, 0.552, 0.555]
y = np.array([new_raw_1, new_raw_2, new_raw_4, new_raw_8])
y = np.transpose(y)
y_mean_new = [np.mean(new_raw_1), np.mean(new_raw_2), np.mean(new_raw_4), np.mean(new_raw_8)]

xxb = [k + 0.1 for k in xx]
for point in y:
    plt.plot(xxb, point, 'bo')

plt.plot(xx, y_mean_new, color='C0', marker='D', markersize=10, markerfacecolor='white', label='Avg A')
plt.plot(xx, y_mean, color='C1', marker='H', markersize=10, markerfacecolor='white', label='Avg B')

leg = plt.legend()

【讨论】:

  • 感谢您的回复。但是在运行上述代码时,我总是遇到以下错误 plt.xticks(xx,x):TypeError: 'int' object is not iterable.
  • from numpy import * meh
  • @JimWang 你没有使用 Python 2.7 是吗?这对我来说在 3.8 中运行良好。
  • @SimonN 是的,原来是python版本的问题。我为 jupyter notebook 添加了 python3,它现在可以工作了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 2018-06-04
相关资源
最近更新 更多