# plotly_week1 **Repository Path**: jiayingb/plotly_week1 ## Basic Information - **Project Name**: plotly_week1 - **Description**: No description available - **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 plotly_01

导入4个城市近17年的婚姻登记数据

In [18]:
import pandas as pd
from plotly.graph_objs import Scatter, Layout
df = pd.read_csv("cities_mattied.csv")
df.head()
Out[18]:
City 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
0 Beijing 2.77 2.80 3.1 3.30 3.40 17.10 11.79 14.75 18.18 13.80 17.32 17.41 16.37 17.00 16.60 16.62 15.15
1 Guangdong 58.90 62.13 57.9 49.70 51.93 85.65 78.79 73.95 73.90 83.27 88.31 86.65 86.22 85.64 85.27 75.04 77.80
2 Shanghai 9.30 9.10 10.8 12.49 10.30 16.60 12.01 14.16 14.99 13.00 14.89 14.42 14.95 14.19 14.18 12.52 10.87
3 Fujian 27.10 29.50 28.0 25.60 25.28 38.19 38.28 38.20 36.06 36.49 35.09 32.90 39.14 31.46 34.94 37.53 39.59
In [19]:
dfc = df.set_index("City")
dfc.head()
Out[19]:
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
City
Beijing 2.77 2.80 3.1 3.30 3.40 17.10 11.79 14.75 18.18 13.80 17.32 17.41 16.37 17.00 16.60 16.62 15.15
Guangdong 58.90 62.13 57.9 49.70 51.93 85.65 78.79 73.95 73.90 83.27 88.31 86.65 86.22 85.64 85.27 75.04 77.80
Shanghai 9.30 9.10 10.8 12.49 10.30 16.60 12.01 14.16 14.99 13.00 14.89 14.42 14.95 14.19 14.18 12.52 10.87
Fujian 27.10 29.50 28.0 25.60 25.28 38.19 38.28 38.20 36.06 36.49 35.09 32.90 39.14 31.46 34.94 37.53 39.59
In [20]:
[int(x) for x in dfc.columns]
Out[20]:
[2001,
 2002,
 2003,
 2004,
 2005,
 2006,
 2007,
 2008,
 2009,
 2010,
 2011,
 2012,
 2013,
 2014,
 2015,
 2016,
 2017]
In [21]:
dfc.loc["Beijing",:]
Out[21]:
2001     2.77
2002     2.80
2003     3.10
2004     3.30
2005     3.40
2006    17.10
2007    11.79
2008    14.75
2009    18.18
2010    13.80
2011    17.32
2012    17.41
2013    16.37
2014    17.00
2015    16.60
2016    16.62
2017    15.15
Name: Beijing, dtype: float64
In [22]:
dfc.loc["Beijing",:].name
Out[22]:
'Beijing'
In [23]:
dfc.loc["Beijing",:].values
Out[23]:
array([ 2.77,  2.8 ,  3.1 ,  3.3 ,  3.4 , 17.1 , 11.79, 14.75, 18.18,
       13.8 , 17.32, 17.41, 16.37, 17.  , 16.6 , 16.62, 15.15])
In [24]:
dfc.loc["Beijing",:].index
Out[24]:
Index(['2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009',
       '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017'],
      dtype='object')
In [32]:
import plotly as py
import plotly.graph_objs as go
In [33]:
trace0 = Scatter(
    x=[int(x) for x in dfc.columns], #列表推导将字符串转换成整数
    y=dfc.loc["Beijing",:].values
)
In [34]:
BJ= go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Beijing",:].values,
    name='北京'
)
GD= go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Guangdong",:].values,
    name='广东'
)

SH=go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Shanghai",:].values,
    name="上海"

)
FJ=go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Fujian",:].values,
    name="福建"

)
py.offline.iplot([BJ,GD,SH,FJ],filename="Bj-Sh-Gd-fj.html")

加入时间序列之滑动选择控件

In [35]:
BJ= go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Beijing",:].values,
    name='北京'
)
GD= go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Guangdong",:].values,
    name='广东'
)

SH=go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Shanghai",:].values,
    name="上海"

)
FJ=go.Scatter(
    x=[int(x) for x in dfc.columns],
    y=dfc.loc["Fujian",:].values,
    name="福建"

)

layout=dict(xaxis=dict(rangeselector=dict(buttons=list([
                                            dict(count=5,
                                                 label="5年",
                                                 stepmode="backward"),
                                            dict(count=10,
                                                 label="10年",
                                                 stepmode="backward"),
                                            dict(count=20,
                                                 label="20年",
                                                 stepmode="backward"),
                                            dict(step="all")
                                            ])),
                       rangeslider=dict(bgcolor="#FFF8DC"),
                       title='年份'
                      ),
              yaxis=dict(title='结婚登记(万对)'),
              title='4个城市结婚登记对比'
             )
fig = dict(data=[BJ,GD,SH,FJ],layout=layout)
py.offline.iplot(fig, filename = 'output_slider')                 
In [ ]: