分组 X 轴离散变量 Pyplot

Grouped X -axis discrete variable Pyplot

我正在为我的公司调整机器,我正在制作一份关于机器与参考相比的偏移量的报告。

我想可视化我所做的措施的手段和不确定性。我在每台机器的 3 个不同温度下进行了 3 次测量,我想在 1 个图表中可视化所有这些结果。我目前每个温度范围都有一张图表。 这是我目前拥有的图片:

我尽力在这里表达我想要的:

有人能帮我吗?

你会注意到我的数据可能没有以正确的方式构建来获得我想要的结果,如果你们也对此有洞察力,我将不胜感激。

import matplotlib.pyplot as plt
import pandas as pd
  

MachineName= ["54711819",   "54711829", "54713465", "54713479", "54713491", "54713489", "54711832", "54711840", "54713470", "54711807", "54711831", "54711824"] 

## Results at 23 degrees ##
Means= [0.120443092,0.159498955,0.134366394,0.12231916,0.251042458,0.146193291,-0.027391228,0.036317345,0.215634285, 0.20710271,0.137419749,0.160709506] ## 23 degré 
Incertitudek2 = [0.0126929,0.014290776,0.018074659,0.0178294,   0.016065171,0.027976306,0.015061493,0.019834669,0.026091469,0.017817986,0.021138409,0.016838815] ## 23 degré 

## Results at 38 degrees ##
#Means= [0.1365,0.2642,0.0511,0.0929,0.3322,0.1179,0.0380,-0.05033,0.1530,0.229472906,0.206441519,0.071041205] ## 38 degré
#Incertitudek2 = [0.0260,0.0707,0.0364,0.0247,0.0660,0.0320,0.0406,0.0615,0.0331,0.027292924,0.035794638,0.024939662] ## 38 degré

## Results at 8 degrees ##
#Means= [0.1087,-0.0625,0.1808,0.1400,0.0986,0.1576,-0.1249,0.1309,0.2684,0.137355552,-0.010638585,0.271221119] ## 8 degré
#Incertitudek2 = [0.0246,0.0379,0.0190,0.0268,0.0355,0.0296,0.0334,0.0250,0.0324,0.025887426,0.033329347,0.030091242] ## 8 degré

plt.errorbar(MachineName, Means, yerr =Incertitudek2,fmt='o',ecolor = 'black',color='red', elinewidth=3)
plt.axhline(y=0.0, color='r', linestyle='-')
plt.grid()
plt.xlabel("Sonde #")
plt.ylabel("écart de température (°C)")
plt.title("[Range 23°C] Calcul sur 50 points par rapport à une sonde référence incertitude K=2")
plt.ylim((-0.2,0.42))
plt.show()

我敢肯定,使用 Pandas 有一种很好且快速的方法可以做到这一点,但这个答案将向您展示如何使用 Matplotlib 获得它。请注意,我不会说法语,所以您可能需要更改标题:)

第一个解决方案:与您提出的方案略有不同,因为它显示的是图例而不是辅助 x-axis。您可以使用 offsetfigsize 的值来增加或减少图表的间距。

MachineName = ["54711819",   "54711829", "54713465", "54713479", "54713491", "54713489", "54711832", "54711840", "54713470", "54711807", "54711831", "54711824"]
Means_23 = [0.120443092,0.159498955,0.134366394,0.12231916,0.251042458,0.146193291,-0.027391228,0.036317345,0.215634285, 0.20710271,0.137419749,0.160709506]
Incertitudek2_23 = [0.0126929,0.014290776,0.018074659,0.0178294,   0.016065171,0.027976306,0.015061493,0.019834669,0.026091469,0.017817986,0.021138409,0.016838815]
Means_38 = [0.1365,0.2642,0.0511,0.0929,0.3322,0.1179,0.0380,-0.05033,0.1530,0.229472906,0.206441519,0.071041205]
Incertitudek2_38 = [0.0260,0.0707,0.0364,0.0247,0.0660,0.0320,0.0406,0.0615,0.0331,0.027292924,0.035794638,0.024939662]
Means_8 = [0.1087,-0.0625,0.1808,0.1400,0.0986,0.1576,-0.1249,0.1309,0.2684,0.137355552,-0.010638585,0.271221119]
Incertitudek2_8 = [0.0246,0.0379,0.0190,0.0268,0.0355,0.0296,0.0334,0.0250,0.0324,0.025887426,0.033329347,0.030091242]

x = np.arange(0, len(MachineName))

plt.figure(figsize=(13, 5))
offset = 0.15
plt.errorbar(x-offset, Means_8, yerr=Incertitudek2_8,fmt='o', elinewidth=3, label="8°C")
plt.errorbar(x, Means_23, yerr=Incertitudek2_23,fmt='o', elinewidth=3, label="23°C")
plt.errorbar(x+offset, Means_38, yerr=Incertitudek2_38,fmt='o', elinewidth=3, label="38°C")
plt.axhline(y=0.0, color='r', linestyle='-')
plt.grid()
plt.xlabel("Sonde #")
plt.ylabel("écart de température (°C)")
plt.title("[Range 23°C] Calcul sur 50 points par rapport à une sonde référence incertitude K=2")
plt.xticks(x, MachineName)
plt.ylim((-0.2,0.42))
plt.legend()
plt.show()

第二个解决方案:为了更接近您的建议,我们需要添加一个辅助x-axis。

x = np.arange(0, len(MachineName))
f = plt.figure(figsize=(13, 5))
ax1 = f.add_subplot(1, 1, 1)
ax2 = ax1.twiny()
ax2.xaxis.set_ticks_position("bottom")
ax2.xaxis.set_label_position("bottom")

offset_cat = 0.25
offset_axis_label = 40

ax1.errorbar(x-offset_cat, Means_8, yerr=Incertitudek2_8,fmt='o', elinewidth=3)
ax1.errorbar(x, Means_23, yerr=Incertitudek2_23,fmt='o', elinewidth=3)
ax1.errorbar(x+offset_cat, Means_38, yerr=Incertitudek2_38,fmt='o', elinewidth=3)
ax1.axhline(y=0.0, color='r', linestyle='-')
ax1.grid()
ax1.set_xlabel("Sonde #", labelpad=offset_axis_label/3)
ax1.set_ylabel("écart de température (°C)")
ax1.set_title("[Range 23°C] Calcul sur 50 points par rapport à une sonde référence incertitude K=2")
ax1.set_xticks(x, MachineName)
ax1.tick_params(axis='x', direction='out', pad=offset_axis_label)
ax1.set_xlim(x[0] - 1, x[-1] + 1)
ax1.set_ylim((-0.2,0.42))

xticks = np.zeros(3 * len(MachineName))
xtick_labels = []
for i in range(len(MachineName)):
    xticks[3*i:3*i+3] = [i - offset_cat, i, i + offset_cat]
    # NOTE: these labels must have the same order of the ax1.errorbar commands
    xtick_labels += ["8°C", "23°C", "38°C"]
ax2.set_xticks(xticks, xtick_labels, rotation=90)
ax2.set_xlim(x[0] - 1, x[-1] + 1)

plt.show()