Home > Software design >  Grouped X -axis discrete variable Pyplot
Grouped X -axis discrete variable Pyplot

Time:04-24

I am adjusting machines for my company and i am making a report about the offset of the machines when compared to a reference.

I want to visualize the means and the uncertainity of the measures i've done. I did the measures 3 times at 3 differents temperature for each machines and i would like to visualize all of those results within 1 graph. I currently have one graph for each temperature range. Here is a picture of what i currently have : Example of 1 of the 3 graph i currently have

I did my best to represent what i want here:

Example of the result i'd like to have

Is anyone able to help me?

You'll notice my data is probably not structured in the right way to obtain the result i want, if you guys have insight on that too it would be super appreciated.

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

CodePudding user response:

I'm sure there is a nice and quick way of doing it with Pandas, but this answer is going to show you how to get it with Matplotlib. Note, I don't speak French, so you probably need to change the title :)

First Solution: a little bit different than what you proposed, as it shows a legend instead of a secondary x-axis. You can play with the values of offset and figsize to give more or less spacing to the chart.

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

enter image description here

Second solution: to get closer to what you proposed, we need to add a secondary 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()

enter image description here

  • Related