# hw-plotly **Repository Path**: NFUNM049/hw-plotly ## Basic Information - **Project Name**: hw-plotly - **Description**: 第一次plotly作业 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-11-09 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 20191108plotly作业
In [31]:
import plotly as py
import pandas as pd
py.offline.init_notebook_mode()
In [52]:
df = pd.read_csv('zaolin2.csv')
In [33]:
df.columns
Out[33]:
Index(['地区', '2017', '2016', '2015', '2014', '2013', '2012', '2011', '2010',
       '2009'],
      dtype='object')
In [51]:
dfc = df.set_index('地区')
In [35]:
dfe = dfc
dfe.head()
Out[35]:
2017 2016 2015 2014 2013 2012 2011 2010 2009
地区
北京市 40.34 19.06 20.33 22.94 45.81 35.75 20.80 13.89 17.6
天津市 12.22 9.29 8.03 7.06 5.79 5.36 7.40 11.32 15.7
河北省 481.27 583.36 366.52 340.04 318.74 312.36 286.42 283.88 306.4
山西省 311.97 266.69 285.94 303.50 298.80 302.85 299.71 282.37 326.6
内蒙古自治区 680.45 618.48 704.05 559.25 805.16 781.62 731.84 655.18 861.9
In [50]:
import plotly as py
import plotly.graph_objs as go
py.offline.init_notebook_mode()

北京 = go.Scatter(
    x=[pd.to_datetime('01/01/{y}'.format(y=x), format="%m/%d/%Y") for x in dfe.columns.values],
    y=dfe.loc["北京市",:].values,
    name = "北京市"
)

天津 = go.Scatter(
    x=[pd.to_datetime('01/01/{y}'.format(y=x), format="%m/%d/%Y") for x in dfe.columns.values],
    y=dfe.loc["天津市",:].values,
    name = "天津市"
)

layout = dict(xaxis=dict(rangeselector=dict( buttons=list([
                                                dict(count=3,
                                                     label="3年",
                                                     step="year",
                                                     stepmode="backward"),
                                                dict(count=5,
                                                     label="5年",
                                                     step="year",
                                                     stepmode="backward"),
                                                dict(count=10,
                                                     label="10年",
                                                     step="year",
                                                     stepmode="backward"),
                                                dict(step="all")
                                            ])),
                         rangeslider=dict(bgcolor="#c3ddff"),
                         title='年份'
                        ),
              yaxis=dict(title='天津与北京近十年造林面积对比'),
              title="近十年北京市与天津市2009年-2017年造林面积对比"               
             )

abc = dict(data=[北京,天津], layout=layout) 

py.offline.iplot(abc, filename = "test.html")
In [ ]: