Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---
- # jupyter:
- # jupytext_format_version: '1.2'
- # jupytext_formats: ipynb,py
- # kernelspec:
- # display_name: Python 3
- # language: python
- # name: python3
- # language_info:
- # codemirror_mode:
- # name: ipython
- # version: 3
- # file_extension: .py
- # mimetype: text/x-python
- # name: python
- # nbconvert_exporter: python
- # pygments_lexer: ipython3
- # version: 3.6.5
- # toc:
- # base_numbering: 1
- # nav_menu: {}
- # number_sections: true
- # sideBar: true
- # skip_h1_title: false
- # title_cell: Table of Contents
- # title_sidebar: Contents
- # toc_cell: false
- # toc_position: {}
- # toc_section_display: true
- # toc_window_display: false
- # ---
- # %autosave 0
- df['doy']
- # +
- import time
- from datetime import datetime
- import pprint
- import pandas as pd
- import ee; ee.Initialize()
- # connection
- import pymongo
- from pymongo import MongoClient
- mongourl = 'mongodb://admin:snap1234@ds251362.mlab.com:51362/snapdb-test'
- client = MongoClient(mongourl)
- db = client.get_database()
- cur = db.variables.find({'District':'Krishna'})
- df = pd.DataFrame(list(cur))
- # -
- df.loc[(df['doy'] >= 152) & (df['doy'] <= 320)]
- dir('334')
- # +
- df['year'] = df['idate'].apply(lambda x: datetime.fromtimestamp(x).strftime('%Y'))
- df['doy'] = df['idate'].apply(lambda x: int(datetime.fromtimestamp(x).strftime('%j')))
- df = df.loc[(df['doy'] > 152) & (df['doy'] < 320)]
- df = df.sort_values(by=['doy'])
- df['mmdd'] = df['idate'].apply(lambda x: datetime.fromtimestamp(x).strftime('%b-%d'))
- lastyear = df.loc[df['year'] == '2017'].groupby(['doy','mmdd','District'])['value'].mean().reset_index()
- thisyear = df.loc[df['year'] == '2018'].groupby(['doy','mmdd','District'])['value'].mean().reset_index()
- vmin = df.groupby(['doy','mmdd','District'])['value'].quantile(0.30).reset_index()
- vmax = df.groupby(['doy','mmdd','District'])['value'].quantile(0.70).reset_index();
- import plotly
- plotly.offline.init_notebook_mode() # run at the start of every notebook
- import plotly.graph_objs as go
- trace0 = go.Scatter(
- x=vmin['mmdd'],
- y=vmin['value'] / 10,
- fill= None,
- # mode='lines',
- name = 'quantile>30%',
- line=dict(
- width = 0.1,
- color='rgb(143, 19, 131)',
- dash = 'dash'
- )
- )
- trace1 = go.Scatter(
- x=vmax['mmdd'],
- y=vmax['value'] / 10,
- fill='tonexty',
- # mode='lines',
- name = 'quantile<70%',
- line=dict(
- width = 0,
- color='rgb(143, 19, 131)',
- dash = 'dash'
- )
- )
- trace2 = go.Bar(
- x = lastyear['mmdd'],
- y = lastyear['value'] / 10,
- # mode = 'markers',
- name = 'year 2017',
- # line=dict(
- # color='rgb(0, 255, 0)',
- # )
- )
- trace3 = go.Bar(
- x = thisyear['mmdd'],
- y = thisyear['value'] / 10,
- # mode = 'markers',
- name = 'year 2018',
- # line=dict(
- # color='rgb(255, 0, 0)',
- # )
- )
- data = [trace0, trace1, trace2, trace3]
- legend=dict(
- x=0,
- y=1,
- traceorder='normal',
- font=dict(
- family='sans-serif',
- size=12,
- color='#000'
- ),
- bgcolor='#E2E2E2',
- bordercolor='#FFFFFF',
- borderwidth=2
- )
- layout = go.Layout(
- title='Krishna District: LAI',
- legend = legend,
- xaxis=dict(
- title='Date',
- hoverformat='.3f',
- titlefont=dict(
- family='Courier New, monospace',
- size=18,
- color='#7f7f7f'
- )
- ),
- yaxis=dict(
- title='Value',
- hoverformat='.3f',
- titlefont=dict(
- family='Courier New, monospace',
- size=18,
- color='#7f7f7f'
- )
- )
- )
- fig = go.Figure(data=data, layout=layout)
- plotly.offline.iplot(fig, filename='basic-area-no-bound')
- # -
- pd.DataFrame({'vmin':vmin, 'vmax':vmax, 'lastyear':lastyear, 'thisyear': thisyear})
- vmin = vmin.rename(index=str, columns={"value": "quantile>30%"})
- vmax = vmax.rename(index=str, columns={"value": "quantile<70%"})
- lastyear = lastyear.rename(index=str, columns={"value": "2017"})
- thisyear = thisyear.rename(index=str, columns={"value": "2018"})
- dataframe = lastyear.merge(thisyear,how='left').merge(vmin,how='left').merge(vmax,how='left')
- dataframe.head()
- dataframe
- vmax.head()
Add Comment
Please, Sign In to add comment