plotly Visualization Quick Tutorial

Links to the original text: Plotly Installs and Draws Five Mainstream Data Diagrams - Huizhi Network

Plotly is a new generation of Python data visualization development library, which provides perfect interactive ability and flexible drawing options. This article will introduce how novices install plotly and write the first plotly drawing program, and use plotly to draw five common data charts.

Compared with Matplotlib and Scalor, Plotly upgrades data visualization to a new level. Plotly has built-in interactive capabilities and editing tools, supports online and offline modes, and provides a stable API for integration with existing applications. It can display data charts in web browsers as well as store local copies. The only disadvantage of Plotly is that it is too flexible and offers too many options.

Learn to program. Huizhi Network Online practice environment, one-to-one teaching assistants answer questions.

1. Installation of PLotly

Plotly is a platform running in JSON format. In Python, we can use the plot.ly package to access the API. Open a terminal software and enter the following command to install plotly:

~$ pip install plotly

Plotly's diagrams are hosted by online web services, so you need to create an online account to save your diagrams first. To extract your personal API KEY, please visit this link: https://plot.ly/settings/api#/. Get the API KEY can then be initialized using the set_credential_files() function, for example:

import plotly

plotly.tools.set_credentials_file(
    username='YourAccountName',       # Account Name
    api_key='YourAPIKey'              # api key
)

2. The Basis of Plotly Data Structure

As mentioned earlier, plotly visualization is based on JSON data structures.

import plotly.plotly as py            # Used to communicate with plotly server
import plotly.graph_objs as go        # Used to generate graphical objects

The graph_objs class contains some general data structures that are consistent across different types of visualization.

Let's start with trace, which is a separate layer of data and drawing instructions. Here's an example of the trace structure:

trace1 = {
  "x": ["2017-09-30", "2017-10-31", "2017-11-30", ...], 
  "y": [327900.0, 329100.0, 331300.0, ...], 
  "line": {
    "color": "#385965", 
    "width": 1.5
  }, 
  "mode": "lines", 
  "name": "Hawaii", 
  "type": "scatter", 
}

As you can see, trace is a dictionary that holds the data to be drawn, as well as the drawing information of color, linearity, and so on.

We can organize multiple traces with a list, which becomes data. The order of traces in data determines the order in which they are placed in the final chart. A typical data looks like this:

data = [trace1, trace2, trace3, trace4]

Layout is used to set the layout of data charts, which includes display features such as headings, axis headings, fonts, etc. Like trace, layout is also a dictionary object:

layout = {
  "showlegend": True, 
  "title": {"text": "Zillow Home Value Index for Top 5 States"}, 
  "xaxis": {
    "rangeslider": {"visible": True}, 
    "title": {"text": "Year from 1996 to 2017"}, 
    "zeroline": False
  }, 
  "yaxis": {
    "title": {"text": "ZHVI BottomTier"}, 
    "zeroline": False
  }
}

Finally, we can use go.Figure() to compile data and layout, and the result will be passed to the drawing function of our choice.

fig = go.Figure(data = data, layout = layout)

3. Plotly bar graph

The following code draws a bar chart:

#Bar Chart
#Mean house values by Bedrooms type and year
import plotly.graph_objs as go
import plotly.plotly as py
trace1 = go.Bar(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_1bedroom,
    name = "ZHVI_1bedroom",
    marker = dict(color = 'rgb(102,255,255)'),
    text = df_groupby_datebr['ZHVI_1bedroom'])
trace2 = go.Bar(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_2bedroom,
    name = "ZHVI_2bedroom",
    marker = dict(color = 'rgb(102,178,255)'),
    text = df_groupby_datebr['ZHVI_2bedroom'])
trace3 = go.Bar(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_3bedroom.values,
    name = "ZHVI_3bedroom",
    marker = dict(color = 'rgb(102,102,255)'),
    text = df_groupby_datebr['ZHVI_3bedroom'])
trace4 = go.Bar(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_4bedroom.values,
    name = "ZHVI_4bedroom",
    marker = dict(color = 'rgb(178, 102, 255)'),
    text = df_groupby_datebr['ZHVI_4bedroom'])
trace5 = go.Bar(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_5BedroomOrMore.values,
    name = "ZHVI_5BedroomOrMore",
    marker = dict(color = 'rgb(255, 102, 255)'),
    text = df_groupby_datebr['ZHVI_5BedroomOrMore'])
data = [trace1, trace2, trace3, trace4, trace5]
layout = go.Layout(barmode = "group", title="Bar Chart: Mean House Values by Bedrooms and Year",
                   xaxis= dict(title= 'Year',ticklen= 5,zeroline= False), 
                   yaxis= dict(title= 'Mean House Values',ticklen= 5,zeroline= False))
fig = go.Figure(data = data, layout = layout)
url = py.plot(fig, validate=False)

Use go.Bar() to create a bar chart type chart. Using the go.Layout() function, we can specify some important information, such as barmode = group, which can group different bars by year, and so on.

4. Plotly Linear Chart

The following code draws a line graph:

#Line Plot
#Mean house values by bedrooms and year
trace1 = go.Scatter(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_1bedroom,
    mode = "lines+markers",
    name = "ZHVI_1bedroom",
    marker = dict(color = 'rgb(102,255,255)'),
    text = df_groupby_datebr['ZHVI_1bedroom'])
trace2 = go.Scatter(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_2bedroom,
    mode = "lines+markers",
    name = "ZHVI_2bedroom",
    marker = dict(color = 'rgb(102,178,255)'),
    text = df_groupby_datebr['ZHVI_2bedroom'])
trace3 = go.Scatter(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_3bedroom.values,
    mode = "lines+markers",
    name = "ZHVI_3bedroom",
    marker = dict(color = 'rgb(102,102,255)'),
    text = df_groupby_datebr['ZHVI_3bedroom'])
trace4 = go.Scatter(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_4bedroom.values,
    mode = "lines+markers",
    name = "ZHVI_4bedroom",
    marker = dict(color = 'rgb(178, 102, 255)'),
    text = df_groupby_datebr['ZHVI_4bedroom'])
trace5 = go.Scatter(
    x = df_groupby_datebr.index.values,
    y = df_groupby_datebr.ZHVI_5BedroomOrMore.values,
    mode = "lines+markers",
    name = "ZHVI_5BedroomOrMore",
    marker = dict(color = 'rgb(255, 102, 255)'),
    text = df_groupby_datebr['ZHVI_5BedroomOrMore'])
data = [trace1, trace2, trace3, trace4, trace5]
layout = go.Layout(title = 'Line Plot: Mean House Values by Bedrooms and Year',
    xaxis= dict(title= 'Year',ticklen= 5,zeroline= False),
    yaxis= dict(title= 'Mean House Values',ticklen= 5,zeroline= False))
fig = go.Figure(data = data, layout = layout)
url = py.plot(fig, validate=False)

Initialize the line graph trace using go.Scatter(). We can use the mode parameter to modify the tag mode. For example:

mode = "lines+markers"

5. Plotly Sequence Line Diagram

The following code draws a sequence diagram:

#Time Series Line Chart
state_list = df_state.groupby('RegionName')[['ZHVI_BottomTier']].mean().sort_values(
            by='ZHVI_BottomTier', ascending=False)[:5].index.values.tolist()
colors = dict(zip(state_list, sns.color_palette("GnBu_d", len(state_list)).as_hex()))


trace_list = []
for state in state_list:
    trace = go.Scatter(
        y=df_state[df_state['RegionName']==state]['ZHVI_BottomTier'].tolist(),
        x=df_state[df_state['RegionName']==state]['Date'].tolist(),
        mode='lines',
        name=state,
        line = dict(
                    color = colors[state],
                    width = 1.5,
#                    dash = 'dot'
                    )
        )
    trace_list.append(trace)
    
    
layout = go.Layout(
    xaxis=dict(title='Year from 1996 to 2017', zeroline=False, rangeslider=dict(visible=True)),
    yaxis=dict(title='ZHVI BottomTier', zeroline=False),

    title='Zillow Home Value Index for Top 5 States',
    showlegend=True,
    )



fig = go.Figure(data=trace_list, layout=layout)
url = py.plot(fig, validate=False, filename='ZHVI BottomTier')

Here we add a range slider to adjust the data we can include in the main diagram. We also use a dictionary to set different colors for each state. For this purpose, we use the color_palette() function of seaborn. Since plotly does not support RGB tuples, we can use the as_hex() function to convert them to 16-bit code.

6. Plotly Multi-hash graph

The following code draws multiple hash diagrams:

#Multiple Scatter Plots
from plotly import tools
trace1 = go.Scatter(x=df_sts.MedianListingPrice_1Bedroom,
    y=df_sts.MedianListingPrice_2Bedroom, mode='markers',
    name = "1Bedroom&2Bedroom", marker = dict(
          color = 'rgb(102,255,255)'))
trace2 = go.Scatter(x=df_sts.MedianListingPrice_2Bedroom,
    y=df_sts.MedianListingPrice_3Bedroom, mode='markers',
    name = "2Bedroom&3Bedroom", marker = dict(
          color = 'rgb(102,178,255)'))
trace3 = go.Scatter( x=df_sts.MedianListingPrice_3Bedroom,
    y=df_sts.MedianListingPrice_4Bedroom, mode='markers',
    name = "3Bedroom&4Bedroom", marker = dict(
          color = 'rgb(102,102,255)'))
trace4 = go.Scatter(x=df_sts.MedianListingPrice_4Bedroom,
    y=df_sts.MedianListingPrice_5BedroomOrMore, mode='markers',
    name = "4Bedroom&5+Bedroom", marker = dict(
          color = 'rgb(178, 102, 255)'))

fig = tools.make_subplots(rows=2, cols=2, subplot_titles=("1Bedroom & 2Bedroom", "2Bedroom & 3Bedroom",
                                                          "3Bedroom&4Bedroom", "4Bedroom&5+Bedroom"))

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace4, 2, 2)

fig['layout']['xaxis3'].update(title='Median Listing Price')#showgrid=False
fig['layout']['xaxis4'].update(title='Median Listing Price')

fig['layout']['yaxis1'].update(title='Median Listing Price')
fig['layout']['yaxis3'].update(title='Median Listing Price')

fig['layout'].update(height=600, width=600, title='Mutiple Scatter Plots: Median Listing Price of' +
                                                  ' Bedrooms')
url = py.plot(fig, validate=False)

To create this layout, instead of adding traces to a single dictionary, we use make_subplots() The function creates different subgraphs, and then adds trace to the specified location using append_trace().

7. Plotly contour map

Use the following code to draw a contour map:

#Choropleth
ZHVI_state_year = df_state.groupby(['RegionName','year'])[['MedianListingPricePerSqft_AllHomes']].mean()
ZHVI_county_year = df_county.groupby(['RegionName','year'])[['MedianListingPricePerSqft_AllHomes']].mean()

ZHVI_state_2017 = df_state[df_state.year==2017].groupby(['RegionName'])[['MedianListingPricePerSqft_AllHomes']].mean()
ZHVI_county_2017 = df_county[df_county.year==2017].groupby(['RegionName'])[['MedianListingPricePerSqft_AllHomes']].mean()

#%%
values = ZHVI_county_2017['MedianListingPricePerSqft_AllHomes'].tolist()
fips = ZHVI_county_2017['MedianListingPricePerSqft_AllHomes'].index.tolist()

ZHVI_county_2017['MedianListingPricePerSqft_AllHomes'].describe()

colorscale = [
    'rgb(102,255,255)',
    'rgb(102,178,255)',
    'rgb(102,102,255)',
    'rgb(178, 102, 255)',
]


fig = ff.create_choropleth(
    fips=fips, values=values, scope=['usa'],
    binning_endpoints=[80.9, 102.8, 135.5], colorscale=colorscale,
    title='United States', legend_title='ZHVI_BottomTier by County'
)
print('done')
url = py.plot(fig, validate=False, filename='ZHVI_cities')

For contour maps, we can take a shortcut using the graph factory class, which contains a set of shortcut functions for creating complex graphs.

import plotly.figure_factory as ff

When ff.create_choropleth() is called, we pass in a set of FIPS values, or each country, city, or State Geographic Identification Code.

Tags: network Python JSON pip

Posted on Wed, 09 Oct 2019 18:28:27 -0700 by La Parka