Shear and Moment Diagrams with Python and Matplotlib

Shear and momement diagrams are used in engineering to plot the shear and moment along the length of a beam.


In [1]:
import numpy as np
import matplotlib.pyplot as plt

In [2]:
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

#Shear Diagram
ax1.plot([0,5,5,10,10],[1/2,1/2,-1/2,-1/2,0])
ax1.fill_between([0,5,5,10,10],[1/2,1/2,-1/2,-1/2,0], alpha=0.5)
# making the top and right spine invisible:
ax1.spines['top'].set_color('none')
ax1.spines['right'].set_color('none')
# moving bottom spine up to y=0 position:
ax1.xaxis.set_ticks_position('bottom')
ax1.spines['bottom'].set_position(('data',0))
ax1.set_ylabel('V')


#Moment Diagram
ax2.plot([0,5,10],[0,1/4,0])
ax2.fill_between([0,5,10],[0,1/4,0], alpha=0.5)
# making the top and right spine invisible:
ax2.spines['top'].set_color('none')
ax2.spines['right'].set_color('none')
# moving bottom spine up to y=0 position:
ax2.xaxis.set_ticks_position('bottom')
ax2.spines['bottom'].set_position(('data',0))
ax2.set_ylabel('M')

plt.show()



In [3]:
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

#Shear Diagram
ax1.plot([0,2.5,2.5,2.5+3,2.5+3,2.5+3+2],[-20,-20,26,26,-14,-14])
ax1.fill_between([0,2.5,2.5,2.5+3,2.5+3,2.5+3+2],[-20,-20,26,26,-14,-14], alpha=0.5)
# making the top and right spine invisible:
ax1.spines['top'].set_color('none')
ax1.spines['right'].set_color('none')
# moving bottom spine up to y=0 position:
ax1.xaxis.set_ticks_position('bottom')
ax1.spines['bottom'].set_position(('data',0))
ax1.set_ylabel('V')


#Moment Diagram
ax2.plot([0,2.5,2.5+3,2.5+3+2],[0,-50,28,0])
ax2.fill_between([0,2.5,2.5+3,2.5+3+2],[0,-50,28,0],alpha=0.5)
# making the top and right spine invisible:
ax2.spines['top'].set_color('none')
ax2.spines['right'].set_color('none')
# moving bottom spine up to y=0 position:
ax2.xaxis.set_ticks_position('bottom')
ax2.spines['bottom'].set_position(('data',0))
ax2.set_ylabel('M')

plt.show()



In [ ]: