Plotting Activity with Python, matplotlib, and SpyderDue April 30 Moodle![]() | ![]() |
Month | Precipitation (inches)![]() |
---|---|
Jan | 0.70 |
Feb | 0.75 |
Dec | 0.84 |
Oct | 1.31 |
Nov | 1.39 |
Aug | 1.62 |
Mar | 1.85 |
Sept | 1.84 |
July | 1.93 |
June | 2.02 |
Apr | 2.93 |
May | 3.05 |
matplotlib
package, and specifically, the pyplot module.
Recall that in the Python community (that you are now a part of!), the matplotlib.pyplot is often assigned an alias of plt.
# import necessary Python packages
import matplotlib.pyplot as plt
# print a message after the package has been imported successfully
print("import of packages successful")
import of packages successful
# create variables for each month of average precipitation for Boulder, CO
jan = 0.70 * 25.4
feb = 0.75 * 25.4
mar = 1.85 * 25.4
apr = 2.93 * 25.4
may = 3.05 * 25.4
june = 2.02 * 25.4
july = 1.93 * 25.4
aug = 1.62 * 25.4
sept = 1.84 * 25.4
oct = 1.31 * 25.4
nov = 1.39 * 25.4
dec = 0.84 * 25.4
# create a list of the converted monthly variables for the y-axis of your plot
precip = [jan, feb, mar, apr, may, june, july, aug, sept, oct, nov, dec]
# create a list of the month names for the x-axis of your plot
months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
# set plot size for the plot
plt.rcParams["figure.figsize"] = (8, 8)
# create the plot space upon which to plot the data
fig, ax = plt.subplots()
# add the x-axis and the y-axis to the plot
ax.plot(months, precip);
fig.savefig('graph.png')
# set plot size for all plots that follow
plt.rcParams["figure.figsize"] = (8, 8)
# set plot title size for all plots that follow
plt.rcParams['axes.titlesize'] = 20
# create the plot space upon which to plot the data
fig, ax = plt.subplots()
# add the x-axis and the y-axis to the plot
ax.plot(months, precip)
# set plot title
ax.set(title="Average Monthly Precipitation in Boulder, CO")
# add labels to the axes
ax.set(xlabel="Month", ylabel="Precipitation (mm)");
# set plot size for all plots that follow
plt.rcParams["figure.figsize"] = (8, 8)
# create the plot space upon which to plot the data
fig, ax = plt.subplots()
# add the x-axis and the y-axis to the plot
ax.bar(months, precip, color="green")
# set plot title
ax.set(title="Average Monthly Precipitation in Boulder, CO")
# add labels to the axes
ax.set(xlabel="Month", ylabel="Precipitation (mm)");
Test your Python skills to further customize your plot: Recreate the x-axis to use full month name (e.g. January, not Jan) (hint: create a new months list).
Rotate the x-axis markers using plt.setp(ax.get_xticklabels(), rotation=45), so that the spacing along the x-axis is more appealing now that the months are longer.
Change your plot to a scatter plot using ax.scatter() (hint: see ax.bar(months, precip)).
I | Attachment | History | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|---|
![]() |
2000px-Spyder_logo.svg.png | r1 | manage | 167.5 K | 2018-12-04 - 23:46 | JimSkon | |
![]() |
Screenshot_20181125_172816.png | r1 | manage | 26.9 K | 2018-11-25 - 22:29 | JimSkon | |
![]() |
Screenshot_20181125_173311.png | r1 | manage | 42.0 K | 2018-11-25 - 22:34 | JimSkon | |
![]() |
Screenshot_20181125_180533.png | r1 | manage | 36.9 K | 2018-11-25 - 23:05 | JimSkon | |
![]() |
Screenshot_20181204_182646.png | r1 | manage | 3.1 K | 2018-12-04 - 23:44 | JimSkon | |
![]() |
spyder-logo.svg | r1 | manage | 7.2 K | 2018-12-04 - 23:45 | JimSkon |