0

I'm trying to show speed vs velocity vs acceleration. The specific numbers don't really matter, just the concept. The last graph isn't showing a line with a slope of zero (ex: y=10). I want the program to show it with the x values of the graph being the same as the others (1 to 10). The specific numbers don't matter except for the x-values because I won't be showing the y-values. I just want a 0 slope line to be visible T-T

t = np.arange(1, 10, 1)
spd_graph = np.exp(t/2)
velo_graph = 3*t
accel_graph = .5
# plot speed
ax1 = plt.subplot(311,ylabel='Speed')
plt.plot(t, spd_graph,'c')
plt.setp(ax1.get_xticklabels(), fontsize=6)
plt.setp(ax1.get_yticklabels(), visible=False)
plt.ylim(0,30)
plt.xlim(0,10)
# plot velocity
ax2 = plt.subplot(312,ylabel='Velocity')
plt.plot(t,velo_graph,'g')
plt.setp(ax2.get_xticklabels(), fontsize=6)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.ylim(0,30)
plt.xlim(0,10)
# plot acceleration
ax3 = plt.subplot(313,label='Acceleration',xlabel='Time',ylabel='Acceleration')
plt.plot(accel_graph,'b')
plt.setp(ax3.get_xticklabels(), fontsize=6)
plt.setp(ax3.get_yticklabels(), visible=False)
plt.ylim(0,30)
plt.xlim(0,10)

plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
nao
  • 1
  • 2

1 Answers1

1

On subplot ax3, plt.plot(accel_graph, 'b') is trying to plot a single float value, which won't work. You need to either:

  1. make sure accel_graph is an array (or list) the same shape as t, e.g., accel_graph = 0.5*np.ones(t.shape), then plt.plot(t, accel_graph, 'b'), or something similar (e.g., plt.plot([min(t), max(t)], [0.5, 0.5], 'b')

  2. plot a horizontal line; instead of the plt.plot(accel_graph, 'b') line, do something like ax3.axhline(0.5, color='b') (note that this will result in a horizontal line that extends beyond the minimum and maximum t values you have)

t = np.arange(1, 10, 1)
spd_graph = np.exp(t/2)
velo_graph = 3*t
accel_graph = .5

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(6, 6), sharey=False)

# plot speed
ax1.plot(t, spd_graph,'c')
ax1.tick_params(axis='x', labelsize=6)
ax1.axes.yaxis.set_ticklabels([])
ax1.set(ylabel='Speed', ylim=(0, 30), xlim=(0, 10))

# plot velocity
ax2.plot(t, velo_graph,'g')
ax2.tick_params(axis='x', labelsize=6)
ax2.axes.yaxis.set_ticklabels([])
ax2.set(ylabel='Velocity', ylim=(0, 30), xlim=(0, 10))

# plot acceleration
ax3.hlines(accel_graph, t[0], t[-1])
ax3.tick_params(axis='x', labelsize=6)
ax3.axes.yaxis.set_ticklabels([])
ax3.set(xlabel='Time', ylabel='Acceleration', ylim=(0, 1), xlim=(0, 10))

plt.show()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
AJ Biffl
  • 493
  • 3
  • 10