Troubleshooting Your Program
Fine-tune the airfoil plotter.
We'll cover the following
Understanding the issues
You wanted to graph an airfoil, not a giant greater-than sign. Let’s catalog all of the issues:
- The points are not graphed correctly; the points should look like an airfoil
- The x-axis and y-axis are flipped, and the numbers are on top of each other
- The airfoil name did not display
- The background should be white
Matplotlib
is supposed to automatically graph (left to right) from negative x to positive x and (down to up) negative y to positive y. Basically, Matplotlib
is supposed to graph like a normal graph automatically. Fortunately, addressing issue 1 will also address issue 2. We can address issues 3 and 4 as well.
Fixing the issues
Since we are pulling the airfoil coordinates from a website and getting the coordinates as strs
, you might be having an issue where Matplotlib
is trying to graph the str
of the coordinates, not the coordinates as actual numbers. You can solve this issue by casting each x and y value as float(x.strip())
and float(y.strip())
instead of just writing x.strip()
and y.strip()
. It is also common to run into issues when splitting a text file at the beginning and/or end of the file. In your case, you clearly have at least one issue at the end: you definitely assigned plot_title
to the first line of the file, but it is displayed on the graph as an empty str
. There are at least three ways to solve the problem:
- Add an additional
if
statement to verify that line contains more than an emptystr
(Remember how an emptystr
is inherentlyFalse
in Python?) - Add a
found_title
boolean and add anelif
statement to check iffound_title
isTrue
- Use
enumerate()
to assign the plot title if the index is 0 (the first line we read) and use a try-except clause to catch theValueError
if the split tries to happen.
All three options are shown below:
Get hands-on with 1400+ tech skills courses.