1

I am trying to plot the energy consumption profile of an electric vehicle. I am using the elevation profile vs the horizontal distance the vehicle runs along a path. I want to add a second x-axis on top of the plot to represent by each chunk of distance, what the energy consumption value was at that precise location.

This is what I have so far, but it's not precisely what I need:

I know this should be fairly simple as it is only adding a second x-axis that matches with the primary x-axis, but I have wasted an entire day trying to figure out unsuccessfully :(
Any insights will be greatly appreciated.

Code:

fig, ax1 = plt.subplots()
elevation_distance_np = elevation_distance.to_numpy()
plt.plot(elevation_distance_np[:,0], elevation_distance_np[:,1], color = 'blue')
plt.grid(True)
plt.xlabel("Distancia recorrida")
plt.ylabel("Elevación de distancia recorrrida")
axes2 = ax1.twiny()
axes2.set_xticks(suma_kWh_np[::mth.ceil(len(suma_kWh_np)/8)])
plt.title("Elevación vs Distancia Recorrida")
plt.show()
K.Cl
  • 1,615
  • 2
  • 9
  • 18
  • Post the code you've used to create this plot so we can help you better. – K.Cl Apr 19 '21 at 11:48
  • fig, ax1 = plt.subplots() elevation_distance_np = elevation_distance.to_numpy() plt.plot(elevation_distance_np[:,0], elevation_distance_np[:,1], color = 'blue') plt.grid(True) plt.xlabel("Distancia recorrida") plt.ylabel("Elevación de distancia recorrrida") axes2 = ax1.twiny() axes2.set_xticks(suma_kWh_np[::mth.ceil(len(suma_kWh_np)/8)]) plt.title("Elevación vs Distancia Recorrida") plt.show() – Carmen Selva Apr 19 '21 at 18:07
  • that is the portion of the code that I am struggling with to get. – Carmen Selva Apr 19 '21 at 18:07

1 Answers1

0

This is a not so trivial endeavor, as these questions show, so don't feel frustrated for not getting this on your own.

Disclaimer: this is not the most elegant solution, but it works. I made a toy example where the conversion from one axis to the other is obtained by dividing the main by 8.5. Also, I replotted your data on this secondary axis, to set the values of its own X axis to something sensible, then removed this extra line.

x = np.linspace(0, 140)  # Some x values, similar to your range
# Caps them to a minimum of 0
y = np.clip(x * (-1) + 100, a_min=0, a_max=100)  
# Creates something similar to your data
elevation_distance_np = np.hstack((x[:, np.newaxis], y[:, np.newaxis]))
# I guessed some transform. If you don't have a formula, 
# you'll need to interpolate between known values, probably.
suma_kWh_np = x / 8.5  
fig, ax1 = plt.subplots()
# Changed to explicit notation, so we don't go back and forth between them
ax1.plot(elevation_distance_np[:,0], elevation_distance_np[:,1], color = 'blue')
ax1.grid(True)
ax1.set_xlabel("Distancia recorrida")
ax1.set_ylabel("Elevación de distancia recorrrida")
ax2 = ax1.twiny()
# Added a copy of your line, but which will be removed later
extra_line = ax2.plot(suma_kWh_np, elevation_distance_np[:,1], color = 'r')
# Now, we get the x ticks and transform them to kWh.
# Here, I had to remove the first and last points ([1:-1])
# because ax1.get_xticks() returned a range from -20 to 160,
ax2.set_xticks(ax1.get_xticks()[1:-1] / 8.5)
ax1.set_title("Elevación vs Distancia Recorrida")
ax2.lines.pop()  # We remove the temporary line right before plotting
plt.show()

Here's the result.

enter image description here

K.Cl
  • 1,615
  • 2
  • 9
  • 18