Need Help ?

Home / Expert Answers / Other / programming-language-python-help-create-line-plot-line-plotwith-two-lines-4-subplots-y-y-plot-histog

(Answered): Programming Language: Python. Help create line plot, line plotwith two lines, 4 subplots, y-y plot, ...



Programming Language: Python. Help create line plot, line plotwith two lines, 4 subplots, y-y plot, histogram in my JupyterNotebook. My code so far is below. Please show source code andscreenshots. Please use Jupyter Notebook on Anaconda. Please useNumPy arrays and linear equations using matrices. Thank you.

Download the Excel spreadsheet file titledgovt_camp_2013_01_to_2016_03.xlsx and save it to your preferredworking directory. The directory where the Excel .xlsx file isplaced needs to be the same directory you build your Jupyternotebook in. The Excel file contains daily weather data pointsgathered from the USGS weather station in Government Camp, OR fromJan 1, 2009 to Mar 31, 2016. The weather measurements and theirunits are given in the column headers (first row) of the Excelspreadsheet.

Excel sheet:

STATION STATION_NAME Snow Depth (inches) 44 Snowfall Max Temp Min Temp (inches) (°F) (°F) 0 29 27 42 0 42 0 20 41 0 31 12 11

Code so far:

In [1]: import numpy as np import pandas as pd import matplotlib.pyplot as plt Ematplotlib inline In [2]: df = pd.read_excel(In [4]: df[ DATE] = pd.to_datetime(df[ DATE], format=%Y%m%d) df.insert(0, year, pd. DatetimeIndex(df[DATE]).year) d

Create a line plot

Once the data is cleaned up, create a line plot with Matplotlibof precipitation in inches vs. time (in days since Jan 01, 2013).Include an x-axis label, a y-axis label, and a title on your plot.Look back at the column headers in the dataframe df to determinewhich column corresponds to Precipitation (inches). Use indexing topull out all of the values from the precipitation column to buildyour plot.

The resulting plot should look something like the plotbelow:

Precipition vs. Time 4.0 - 3.5 3.0 2.5 Precipitaiton (inches) 2.0 1.5 1.0 - 0.5 0.0 O 200 1000 1200 400 600 800 Time (days si

Create a line plot with two lines

Next, create a Matplotlib line plot that shows the maximum andminimum temperatures in 2013 on two lines. Look back at thedatafrome df to determine which columns correspond to the maximumand minimum temperatures recorded each day. The maximum temp shouldbe a red line and the minimum temp should be a blue line. Include alegend that shows Max Temp and Min Temp. Include axis labels and atitle on the plot.

The resulting plot should look something like the plotbelow:

Max and Min Temperatures in 2013 Max Temp Min Temp 80 60 Temperature in degrees F What 40 50 300 350 100 150 200 250 day of t

Create 4 subplots

The next task is to create a Matplotlib figure containing foursubplots that looks like the figure with four plots shown below.The four subplots should be stacked on top of each other. Eachsubplot should contain the precipitation data vs. time (day of theyear) in one year. The first subplot shows precipitation in 2013,the second subplot shows precipitation in 2014, etc. Include atitle on each subplot and a y-axis label on each subplot. On thebottom subplot (Precipitation in 2016), include an x-axislabel.

Use NumPy’s np.where() function or boolean masks to save theprecipitation data (number of inches only) exclusively for days inthe year 2013 to a NumPy array called precip_2013. Use theplt.subplots() method to plot the precip_2013 data in the firstsubplot.

Complete the same type of indexing operation on theprecipitation data for 2014, then 2015, then 2016. Create a subplotfor each year. The plot for 2016 should include an x-axis label andhave the same axis limits as the x-axis limits in the othersubplots.

Hint: When plt.subplots() is called, include the keywordargument figsize=(9,9). This keyword argument makes the figurelarger and leaves more space for the axis labels and titles of eachsubplot. Modify the numbers (9,9) to change the figure width andheight.

The resulting figure which contains four subplots, one subplotfor each year, should look something like below. The commandplt.tight_layout() can also help with subplot spacing.

2013 Daily Precipitation ????????? ?? ?? 2014 Daily Precipitation ????????? ???? ???? 2015 Daily Precipitation Precipitation

y-y plot

The next plot you will build is a y-y plot. A y-y plot has twodifferent y-axes. Each set or sets of data corresponds to one ofthe separate y-axes. Y-y plots are useful when two types of datacontain the same x-values (like time) but very different scales ofy-values (like snow fall and snow depth).

Use boolean masking to save some of the snow depth data into aNumPy array called winter13_14_sd. Remember which column headerscorrespond to which measurement in the dataframe df. Refer to thedataframe or Excel file, if need be, to see which columncorresponds snow depth. Your snow depth data set should onlyinclude measurements from the first day of November 2013 to thelast day of February 2014. Slicing or indexing out snow depth datain this date range is probably be the most difficult part of thelab.

Hint: Create multiple boolean masks and then combine the booleanmasks with NumPy’s np.logical_and() and np.logical_or() functions.These two NumPy functions operate like Python’s and, or keywords,but can be applied to arrays of boolean values.

Index out the same days’ (Nov 1, 2013 to Feb 27, 2014) snow falldata and save the snow fall data in this date range to a NumPyarray called winter13_14_sf.

Use Matplotlib’s ax2 = ax1.twinx() method to plot both sets ofdata on the same plot (same x-axis), but with different y-axes.

Notice the figure has a different vertical axis on the leftcompared to the vertical axis on the right. Let Matplotlib chooseyour axis limits and line colors. Be sure to include the gridlines, x-axis label, y-axis labels, title, and legend as seen inthe figure below. It is tricky to include both entries in thelegend. Do your best, but don’t waste too much time configuring thelegend.

12 Snow Depth Snow Fall 40 - 10 30 8 Snow Depth (inches) Snow Fall (inches) 20 10 Mata F2 0 0 0 20 40 80 100 120 60 Day # Plo

Histogram

The final plot to create in this lab is a histogram. A histogramis a statistical plot that shows the number of values that fallinto different value ranges, called bins. Use your winter13_14_sfNumPy array to build the histogram. Include 10 bins in thehistogram. Make sure to match all of the labels and title from theexample histogram below.

Number of Days in which (x-axis) Inches of Snow Fell from Nov 2013 to Feb 2014 80 60 # of Days # 40 20 T T T T 0 1 2 3 7 8 9fjdsaklf;ds

STATION STATION_NAME Snow Depth (inches) 44 Snowfall Max Temp Min Temp (inches) (°F) (°F) 0 29 27 42 0 42 0 20 41 0 31 12 11 12 14 28 26 30 32 41 0 37 31 40 0 40 1 34 40 38 0.1 0 38 40 31 42 40 23 26 19 2 GHCND:USC00353402 3 GHCND:USC00353402 4 GHCND:USC00353402 5 GHCND:USC00353402 6 GHCND:USC00353402 7 GHCND:USC00353402 8 GHCND:USC00353402 9 GHCND:USC00353402 10 GHCND:USC00353402 11 GHCND:USC00353402 12 GHCND:USC00353402 13 GHCND:USC00353402 14 GHCND:USC00353402 15 GHCND:USC00353402 16 GHCND:USC00353402 17 GHCND:USC00353402 18 GHCND:USC00353402 19 GHCND:USC00353402 20 GHCND:USC00353402 21 GHCND:USC00353402 22 GHCND:USC00353402 23 GHCND:USC00353402 24 GHCND:USC00353402 25 GHCND:USC00353402 26 GHCND:USC00353402 27 GHCND:USC00353402 Precipitati DATE on (inches) 20130101 0 20130102 0.01 20130103 0 20130104 0.16 20130105 0 20130106 0.31 20130107 0.74 20130108 0.89 20130109 0.22 20130110 0.6 20130111 0.18 20130112 0 20130113 0 20130114 0 20130115 0.01 20130116 0 20130117 0 20130118 0 20130119 0 20130120 0 20130121 0 20130122 0 20130123 0 20130124 0.21 20130125 0.24 20130126 0.62 GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US GOVERNMENT CAMP OR US 43 43 43 42 23 22 6 2 0 0 0 11 9 9 14 26 42 0 27 41 45 0 0 40 31 31 53 60 0 0 21 30 34 35 34 58 63 28 0 28 0 49 0 57 24 23 31 29 30 26 50 23 0 0.1 0 41 40 23 21 24 27 0.1 39 In [1]: import numpy as np import pandas as pd import matplotlib.pyplot as plt Ematplotlib inline In [2]: df = pd.read_excel('govt_camp_2013_01_to_2016_03.xlsx') df.head() Out[2]: STATION STATION_NAME DATE Precipitationn(inches) Snow Depthn(inches) Max Min Snowfalln(inches) Temp Temp (°F) (°F) 0 GHCND:USC00353402 GOVERNMENT CAMP OR US 20130101 0.00 44 0.0 29 12 1 GHCND:USC00353402 GOVERNMENT CAMP OR US 20130102 0.01 42 0.0 27 11 2 GHCND:USC00353402 GOVERNMENT CAMP OR US 20130103 0.00 42 0.0 20 12 3 GHCND:USC00353402 GOVERNMENT CAMP OR US 20130104 0.16 41 0.0 31 14 4 GHCND:USC00353402 GOVERNMENT CAMP OR US 20130105 0.00 41 0.0 37 28 In [3]: df.drop ( ['STATION', STATION_NAME'], axis=1, inplace=True) In [4]: df[ 'DATE'] = pd.to_datetime(df[ 'DATE'], format='%Y%m%d') df.insert(0, 'year', pd. DatetimeIndex(df['DATE']).year) df.insert(1, 'month', pd.DatetimeIndex(df [ 'DATE']).month) df.insert(2, 'day', pd. DatetimeIndex(df['DATE']).day) df.drop ( ['DATE'), axis=1, inplace=True) print(df.shape) df.head() (1186, 8) Out[4]: year month day Precipitationn(inches) Snow Depthn(inches) Snowfalln(inches) Max Temp (°F) Min Temp (°F) 0 2013 1 1 0.00 44 0.0 29 12 1 2013 1 2 0.01 42 0.0 27 11 2 2013 1 3 0.00 42 0.0 20 12 3 2013 1 4 0.16 41 0.0 31 14 4 2013 1 5 0.00 41 0.0 37 28 In [5]: wd = np.array(df) print (wd) [[2.013e+03 1.000e+00 1.000e+00 [2.013e+03 1.000e+00 2.000e+00 [2.013e+03 1.000e+00 3.000e+00 0.000e+00 2.900e+01 1.200e+01] 0.000e+00 2.700e+01 1.100e+01 ] 0.000e+00 2.000e+01 1.200e+01] [2.016e+03 3.000e+00 2.900e+01 [2.016e+03 3.000e+00 3.000e+01 [2.016e+03 3.000e+00 3.100e+01 0.000e+00 3.300e+01 2.700e+01 ] 0.000e+00 4.700e+01 2.700e+01] 0.000e+00 5.900e+01 3.200e+01]] Precipition vs. Time 4.0 - 3.5 3.0 2.5 Precipitaiton (inches) 2.0 1.5 1.0 - 0.5 0.0 O 200 1000 1200 400 600 800 Time (days since Jan 01, 2013) Plot 1: Line Plot Max and Min Temperatures in 2013 Max Temp Min Temp 80 60 Temperature in degrees F What 40 50 300 350 100 150 200 250 day of the year (in days) Plot 2: Multi-line Plot 2013 Daily Precipitation ????????? ?? ?? 2014 Daily Precipitation ????????? ???? ???? 2015 Daily Precipitation Precipitation (inches) thunder Wohnhaus mh 2016 Daily Precipitation Precipitation (inches) www.tanah 0 50 100 250 300 350 150 200 Time (day of the year) Plot 3: Four Subplots 12 Snow Depth Snow Fall 40 - 10 30 8 Snow Depth (inches) Snow Fall (inches) 20 10 Mata F2 0 0 0 20 40 80 100 120 60 Day # Plot 4: y-y Plot Number of Days in which (x-axis) Inches of Snow Fell from Nov 2013 to Feb 2014 80 60 # of Days # 40 20 T T T T 0 1 2 3 7 8 9 10 4 5 6 Inches Fallen Plot 5: Histogram


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer to Programming Language: Python. Help create line plot, line plot with two lines, 4 subplots, y-y plot, histogram in my Ju...

The Problem has Answer!

We have detailed solutions for you for more understanding.

View Answer