Skip to content

Instantly share code, notes, and snippets.

@franktoffel
Last active August 29, 2015 14:18
Show Gist options
  • Save franktoffel/843e58486fea9d795400 to your computer and use it in GitHub Desktop.
Save franktoffel/843e58486fea9d795400 to your computer and use it in GitHub Desktop.
A simple plug flow reactor figure using Python (matplotlib)
# coding: utf-8
"""
Plug flow reactor figure created with Python
This example displays a simple representation of a plug flow reactor (PFR)
by means of matplotlib API. A full list of artists and the documentation is
available at http://matplotlib.org/api/artist_api.html.
Copyright (c) 2015, Francisco J. Navarro-Brull
http://CAChemE.org
MIT License
More Chemical Engineering examples can be found here:
https://github.com/cacheme/learn
"""
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Rectangle, Arrow
# In Python comments are indicated with "#"
# Until now we have just imported the libraries to plot the PFR
fig, axes = plt.subplots()
reactor_length = 3 # [m]
reactor_diameter = 1 # [m]
arrow_lenght = 1 # [m]
# Let's begin with the arrow representing the outlet
arrow_outlet = Arrow(reactor_length, reactor_diameter/2, # x, y (coordinates)
arrow_lenght, 0, width=0.1, # dx, dy
facecolor="black") # color options
axes.add_patch(arrow_outlet) # adds the path to the plot
# Now we create and plot elliplse representing the end of the cillinder
ellipse_outlet = Ellipse((reactor_length,reactor_diameter/2), # x, y (coordinates)
reactor_diameter/2, reactor_diameter, # width, height
facecolor="grey", edgecolor="black") # color options
axes.add_patch(ellipse_outlet) # adds the patch to the plot
# Create and plot the tube as rectangle
rectangle = Rectangle((0, 0), # x, y (coordinates)
reactor_length, reactor_diameter, # width, height
facecolor="grey", edgecolor="none") # color options
axes.add_patch(rectangle) # adds the patch to the plot
# Again we create an ellipse but this time at the inlet
ellipse_inlet = Ellipse((0,reactor_diameter/2), # x, y (coordinates)
reactor_diameter/2, reactor_diameter, # width, height
facecolor="grey", edgecolor="black") # color options
axes.add_patch(ellipse_inlet) # adds the patch to the plot
# And finally the inlet arrow
arrow_inlet = Arrow(-arrow_lenght, reactor_diameter/2, # x, y (coordinates)
arrow_lenght, 0, width=0.1, # dx, dy, arrow width
facecolor="black") # color options
axes.add_patch(arrow_inlet) # adds the patch to the plot
plt.axis('equal') # forces the same grid space between axes
plt.axis('off') # removes al the ticks and axes (comment this line to better understand the code above)
# axes.set_xlim([-2,4]) # fixes the x axes
# axes.set_ylim([-1,1]) # fixes the y axes
plt.show() # shows the figure
@franktoffel
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment