Difference: PlottingActivity1 (1 vs. 7)

Revision 72019-04-25 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"
Changed:
<
<

Plotting Activity with Python, matplotlib, and Spyder

Due April 30

Moodle

2000px-Spyder_logo.svg.png
>
>

Plotting Activity with Python, matplotlib, and Spyde

2000px-Spyder_logo.svg.png
 

With Spyder and Anaconda Python

In this activity, you will write Python code to plot data from lists using the matplotlib package. Because repl.it seems to have problems, we will learn to do it with Spyder (a python development enviornment, or IDE, and Anaconda Python 3.

Revision 62019-04-23 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"

Plotting Activity with Python, matplotlib, and Spyder

Line: 9 to 9
 

With Spyder and Anaconda Python

In this activity, you will write Python code to plot data from lists using the matplotlib package. Because repl.it seems to have problems, we will learn to do it with Spyder (a python development enviornment, or IDE, and Anaconda Python 3.

Added:
>
>
Her are instructions for how you can install Python on your own computer: PythonInstall
 

First Step - Get Spyder Ready

  1. Click on the windows icon and type "spyder". Select the program Screenshot_20181204_182646.png.
  2. This is a local version of Python, and it works much like repl.it.

Revision 52019-03-24 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"

Plotting Activity with Python, matplotlib, and Spyder

Revision 42019-03-18 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"

Plotting Activity with Python, matplotlib, and Spyder

Changed:
<
<
Due Dev 11
>
>
Due April 30
 
Changed:
<
<
Moodle
2000px-Spyder_logo.svg.png
>
>
Moodle
2000px-Spyder_logo.svg.png
 

With Spyder and Anaconda Python

In this activity, you will write Python code to plot data from lists using the matplotlib package. Because repl.it seems to have problems, we will learn to do it with Spyder (a python development enviornment, or IDE, and Anaconda Python 3.

Revision 32018-12-04 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"
Changed:
<
<

Plotting Activity with Python and matplotlib

>
>

Plotting Activity with Python, matplotlib, and Spyder

 
Changed:
<
<
In this activity, you will write Python code to plot data from lists using the matplotlib package.
>
>
Due Dev 11
 
Added:
>
>
Moodle
2000px-Spyder_logo.svg.png

With Spyder and Anaconda Python

In this activity, you will write Python code to plot data from lists using the matplotlib package. Because repl.it seems to have problems, we will learn to do it with Spyder (a python development enviornment, or IDE, and Anaconda Python 3.

First Step - Get Spyder Ready

  1. Click on the windows icon and type "spyder". Select the program Screenshot_20181204_182646.png.
  2. This is a local version of Python, and it works much like repl.it.
  3. Put the code on the left. You should save it to a folder "Python" on your network drive.
  4. Run by hitting the green arrow.
  5. You do not have to use the ".savefig()" method on the plot anymore, unless you actually want to save the plot to a file.
  6. YOu will always need to save your file before you can run it.
  7. Even though they probably won't work, take you finished work and put it in a new repl.it. and turn in that link.
 

Types of Plots

Plots are very useful for displaying information that has a temporal occurrence such as the monthly precipitation data. There are many different kinds of plots including line and bar graphs as well as scatter plots (i.e. plots of points representing observations in the data).

Line: 111 to 119
 

Screenshot_20181125_173311.png

Deleted:
<
<
 

Change Plot Type

Changed:
<
<
You can turn your plot into a bar plot using ax.bar(), providing the x- and y-axes as you did with ax.plot().

You can also assign a fill color using color="green".

>
>
You can turn your plot into a bar plot using ax.bar(), providing the x- and y-axes as you did with ax.plot().
 
Added:
>
>
You can also assign a fill color using color="green".
 
# set plot size for all plots that follow
plt.rcParams["figure.figsize"] = (8, 8)

Line: 134 to 140
 ax.set(xlabel="Month", ylabel="Precipitation (mm)");
Changed:
<
<
Test your Python skills to further customize your plot:
  1. Recreate the x-axis to use full month name (e.g. January, not Jan) (hint: create a new months list).

  2. 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.

  3. Change your plot to a scatter plot using ax.scatter() (hint: see ax.bar(months, precip)).

>
>
Test your Python skills to further customize your plot:
  1. Recreate the x-axis to use full month name (e.g. January, not Jan) (hint: create a new months list).

  2. 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.

  3. Change your plot to a scatter plot using ax.scatter() (hint: see ax.bar(months, precip)).

 Screenshot_20181125_180533.png

From: https://www.earthdatascience.org/courses/earth-analytics-bootcamp/python-variables-lists/plot-data-matplotlib/

Added:
>
>
Screenshot_20181204_182646.png:
 
META FILEATTACHMENT attachment="Screenshot_20181125_172816.png" attr="" comment="" date="1543184975" name="Screenshot_20181125_172816.png" path="Screenshot_20181125_172816.png" size="27570" user="JimSkon" version="1"
META FILEATTACHMENT attachment="Screenshot_20181125_173311.png" attr="" comment="" date="1543185254" name="Screenshot_20181125_173311.png" path="Screenshot_20181125_173311.png" size="43046" user="JimSkon" version="1"
META FILEATTACHMENT attachment="Screenshot_20181125_180533.png" attr="" comment="" date="1543187156" name="Screenshot_20181125_180533.png" path="Screenshot_20181125_180533.png" size="37738" user="JimSkon" version="1"
Added:
>
>
META FILEATTACHMENT attachment="Screenshot_20181204_182646.png" attr="" comment="" date="1543967093" name="Screenshot_20181204_182646.png" path="Screenshot_20181204_182646.png" size="3195" user="JimSkon" version="1"
META FILEATTACHMENT attachment="spyder-logo.svg" attr="" comment="" date="1543967126" name="spyder-logo.svg" path="spyder-logo.svg" size="7424" user="JimSkon" version="1"
META FILEATTACHMENT attachment="2000px-Spyder_logo.svg.png" attr="" comment="" date="1543967173" name="2000px-Spyder_logo.svg.png" path="2000px-Spyder_logo.svg.png" size="171492" user="JimSkon" version="1"

Revision 22018-11-25 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="IntroToCSF2018"

Plotting Activity with Python and matplotlib

Changed:
<
<
In this activity, you will write Python code to plot data from lists using the matplotlib package.
>
>
In this activity, you will write Python code to plot data from lists using the matplotlib package.
 

Types of Plots

Plots are very useful for displaying information that has a temporal occurrence such as the monthly precipitation data. There are many different kinds of plots including line and bar graphs as well as scatter plots (i.e. plots of points representing observations in the data).

Changed:
<
<
To create a plot, you need to provide data for the x-axis (i.e. horizontal axis) and the y-axis (i.e. vertical axis) of the plot. The data can be contained in various formats including lists and other data structures such as numpy arrays and pandas dataframes.
>
>
To create a plot, you need to provide data for the x-axis (i.e. horizontal axis) and the y-axis (i.e. vertical axis) of the plot. The data can be contained in various formats including lists and other data structures such as numpy arrays and pandas dataframes.
 
Changed:
<
<
In this lesson, you will use your previously created lists for average monthly precipitation in Boulder, CO to create and customize a plot. You will use months along the x-axis and precip along the y-axis.
>
>
In this lesson, you will use your previously created lists for average monthly precipitation in Boulder, CO to create and customize a plot. You will use months along the x-axis and precip along the y-axis.
  Average Monthly Precipitation for Boulder, Colorado provided by the U.S. National Oceanic and Atmospheric Administration (NOAA)
Month Precipitation (inches)
Line: 28 to 28
 
Dec 0.84

Begin Writing Your Code

Changed:
<
<
Now that you know how to import Python packages, you can begin your code by importing the matplotlibpackage, and specifically, the pyplot module.
>
>
Now that you know how to import Python packages, you can begin your code by importing the matplotlibpackage, 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.

 
Deleted:
<
<
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

Line: 39 to 40
 
import of packages successful

Create Lists

Changed:
<
<
Review your Python skills to create lists of the converted values for average monthly precipitation (mm) and of the month names.
>
>
Review your Python skills to create lists of the converted values for average monthly precipitation (mm) and of the month names.
 
# create variables for each month of average precipitation for Boulder, CO
jan = 0.70 * 25.4
feb = 0.75 * 25.4

Line: 61 to 63
 months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]

Plot Data From Lists

Changed:
<
<
Since you now have lists containing the converted values for average monthly precipitation and the month names, you can use these lists to create a plot using matplotlib.
>
>
Since you now have lists containing the converted values for average monthly precipitation and the month names, you can use these lists to create a plot using matplotlib.
 
Changed:
<
<
Matplotlib is a plotting package that makes it simple to create plots from various data structures in Python, including lists.
>
>
Matplotlib is a plotting package that makes it simple to create plots from various data structures in Python, including lists.
 
Changed:
<
<
Matplotlib uses default settings, which help to create publication quality plots with a minimal amount of settings and tweaking. Matplotlib graphics are built step by step by adding new elements.
>
>
Matplotlib uses default settings, which help to create publication quality plots with a minimal amount of settings and tweaking. Matplotlib graphics are built step by step by adding new elements.
 
Changed:
<
<
To build a matplotlib plot, you need to:
>
>
To build a matplotlib plot, you need to:
 
  1. Create the empty plot on which data will be plotted
  2. Define the plot elements including the x- and y- axes (variables)
  3. Customize your plot to change default styles and to add titles and labels to the axes.
Changed:
<
<
Begin by creating a plot using the default styles provided by matplotlib.pyplot.
>
>
Begin by creating a plot using the default styles provided by matplotlib.pyplot.
 
# set plot size for the plot
plt.rcParams["figure.figsize"] = (8, 8)

Line: 79 to 82
 fig, ax = plt.subplots()

# add the x-axis and the y-axis to the plot

Changed:
<
<
ax.plot(months, precip);
>
>
ax.plot(months, precip); fig.savefig('graph.png')

Screenshot_20181125_172816.png

Customize Your Plot

Add Title and Axis Labels

Expand your code to add a title to the plot and to label the axes.

# 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)");

Screenshot_20181125_173311.png

Change Plot Type

You can turn your plot into a bar plot using ax.bar(), providing the x- and y-axes as you did with ax.plot().

You can also assign a fill color using color="green".

# 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:

  1. Recreate the x-axis to use full month name (e.g. January, not Jan) (hint: create a new months list).

  2. 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.

  3. Change your plot to a scatter plot using ax.scatter() (hint: see ax.bar(months, precip)).

Screenshot_20181125_180533.png
  From: https://www.earthdatascience.org/courses/earth-analytics-bootcamp/python-variables-lists/plot-data-matplotlib/
Added:
>
>
META FILEATTACHMENT attachment="Screenshot_20181125_172816.png" attr="" comment="" date="1543184975" name="Screenshot_20181125_172816.png" path="Screenshot_20181125_172816.png" size="27570" user="JimSkon" version="1"
META FILEATTACHMENT attachment="Screenshot_20181125_173311.png" attr="" comment="" date="1543185254" name="Screenshot_20181125_173311.png" path="Screenshot_20181125_173311.png" size="43046" user="JimSkon" version="1"
META FILEATTACHMENT attachment="Screenshot_20181125_180533.png" attr="" comment="" date="1543187156" name="Screenshot_20181125_180533.png" path="Screenshot_20181125_180533.png" size="37738" user="JimSkon" version="1"

Revision 12018-11-25 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="IntroToCSF2018"

Plotting Activity with Python and matplotlib

In this activity, you will write Python code to plot data from lists using the matplotlib package.

Types of Plots

Plots are very useful for displaying information that has a temporal occurrence such as the monthly precipitation data. There are many different kinds of plots including line and bar graphs as well as scatter plots (i.e. plots of points representing observations in the data).

To create a plot, you need to provide data for the x-axis (i.e. horizontal axis) and the y-axis (i.e. vertical axis) of the plot. The data can be contained in various formats including lists and other data structures such as numpy arrays and pandas dataframes.

In this lesson, you will use your previously created lists for average monthly precipitation in Boulder, CO to create and customize a plot. You will use months along the x-axis and precip along the y-axis.

Average Monthly Precipitation for Boulder, Colorado provided by the U.S. National Oceanic and Atmospheric Administration (NOAA)

Month Precipitation (inches)
Jan 0.70
Feb 0.75
Mar 1.85
Apr 2.93
May 3.05
June 2.02
July 1.93
Aug 1.62
Sept 1.84
Oct 1.31
Nov 1.39
Dec 0.84

Begin Writing Your Code

Now that you know how to import Python packages, you can begin your code by importing the matplotlibpackage, 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 Lists

Review your Python skills to create lists of the converted values for average monthly precipitation (mm) and of the month names.

# 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"]

Plot Data From Lists

Since you now have lists containing the converted values for average monthly precipitation and the month names, you can use these lists to create a plot using matplotlib.

Matplotlib is a plotting package that makes it simple to create plots from various data structures in Python, including lists.

Matplotlib uses default settings, which help to create publication quality plots with a minimal amount of settings and tweaking. Matplotlib graphics are built step by step by adding new elements.

To build a matplotlib plot, you need to:

  1. Create the empty plot on which data will be plotted
  2. Define the plot elements including the x- and y- axes (variables)
  3. Customize your plot to change default styles and to add titles and labels to the axes.
Begin by creating a plot using the default styles provided by matplotlib.pyplot.
# 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);

From: https://www.earthdatascience.org/courses/earth-analytics-bootcamp/python-variables-lists/plot-data-matplotlib/

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback