Home > Software engineering >  Is there a way to create a graph that has iterated pairs of consecutive items in its own x-axis and
Is there a way to create a graph that has iterated pairs of consecutive items in its own x-axis and

Time:11-19

I would like to iterate over all pairs of consecutive items in the list called as ObjectIDs, then I want to plot and demonstrate a 2D graph that shows each slop value between the two specific nodes/points from the list called Slops.

meterBetweenTwoPoints = 5
currentPointDateTimes = []
objectHeights = []
objectIDs = []
objectsIDsForGraphNodes = []
slopeValues = []

..

for x in range(0, len(objectHeights)): 
    objectsIDsForGraphNodes.append(objectIDs[sortedIndexLibrarycurrentPointDateTimes[x]])

print(objectsIDsForGraphNodes)

>> [1480642, 1504454, 1504455, 1504456, 1504457, 1504458]

for x in range(0, len(objectHeights)-1):
    pointXHeight = objectHeights[sortedIndexLibrarycurrentPointDateTimes[x]]
    pointYHeight = objectHeights[sortedIndexLibrarycurrentPointDateTimes[x 1]]
    currentSlopeValue = ((pointYHeight - pointXHeight) / meterBetweenTwoPoints) * 100
    slopeValues.append(currentSlopeValue)

print(slopeValues)

>> [-14.6541400000001, -4.927820000000054, -2.8426600000000235, -2.2563800000000356, -1.0200999999997862]

Therefore, this means that I have 6 different nodes and 5 different slope values.

As you can see, the size of lists is not equal but I can assign an extra "zero" to the list called slopeValues. I can convert the list into data frames but still couldn't manage to make it.

My environment cannot support libs like "plotly".

I prepared a visualization to make my request/demand clear:

enter image description here

This graph represents the desired output. I tried multiple solutions by using matplotlib, seaborn, and pandas but I couldn't manage to execute an output.

In addition to this, is there any better visualization to show the inclination/slope values between to specific points(for roads)?

Thanks in advance, I appreciate it.

CodePudding user response:

import matplotlib.pyplot as plt
X = [1480642, 1504454, 1504455, 1504456, 1504457, 1504458]
Y = [-14.6541400000001, -4.927820000000054, -2.8426600000000235, -2.2563800000000356, -1.0200999999997862]
#Custom X tick text
X_ticks = ['['  str(X[i])   '-'   str(X[i 1])  ']' for i in range(0,len(X)-1)]

# add 0 to make list size equal
X_ticks.insert(0, 0)
Y.insert(0,0)

#Draw Plot
plt.figure(1, figsize = (12,6))
plt.suptitle('Slope Plot')
ax = plt.gca()
x_axis = range(0,len(X_ticks))

ax.set_xlim([0, len(X_ticks)])
ax.set_ylim([0, min(Y) - 2 ])

# Draw horizontal lines
for v in Y:
  plt.axhline(y=v, color='g', linestyle='--')

plt.xticks(x_axis, X_ticks)
plt.yticks(Y)
plt.plot(x_axis, Y, 'ro--')

Here is the plot :- enter image description here

  • Related