{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly\n", "import plotly.graph_objs as go\n", "plotly.offline.init_notebook_mode(connected=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Line Plots" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": true }, "data": [ { "connectgaps": false, "line": { "color": "rgb(205, 12, 24)", "width": 4 }, "mode": "lines", "name": "High 2014", "showlegend": true, "type": "scatter", "uid": "2c78497a-5d08-49bb-ac60-dd8ae8bd729a", "x": [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], "y": [ 28.8, 28.5, null, 40, 69.7, 79.7, 78.5, 77.8, null, 62.6, 45.3, 39.9 ] }, { "line": { "color": "rgb(25, 120, 24)", "dash": "dash", "width": 4 }, "name": "High 2007", "type": "scatter", "uid": "5ad15ae3-029a-4176-80e4-ab4357cc8196", "x": [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], "y": [ 36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76, 67.3, 46.1, 35 ] }, { "line": { "color": "rgb(25, 12, 204)", "dash": "dot", "width": 4 }, "name": "High 2000", "type": "scatter", "uid": "b3ab7e90-cbef-4ba0-974a-037b28e073bf", "x": [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], "y": [ 32.5, 37.6, 49.9, 53, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3 ] } ], "layout": { "title": "Average High and Low Temperatures in New York", "xaxis": { "title": "Month" }, "yaxis": { "title": "Temperature (degrees F)" } } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']\n", "high_2000 = [32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3]\n", "high_2007 = [36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0]\n", "high_2014 = [28.8, 28.5, None, 40.0, 69.7, 79.7, 78.5, 77.8, None, 62.6, 45.3, 39.9]\n", "\n", "trace0 = go.Scatter(\n", " x = month,\n", " y = high_2014,\n", " name = 'High 2014',\n", " # 默认为lines\n", " mode = 'lines', \n", " # 是否连接Gap,图中红色会有缺失部分\n", " connectgaps = False,\n", " # 是否显示legend\n", " showlegend = True,\n", " # 设置线的样式\n", " line = dict(\n", " color='rgb(205, 12, 24)', \n", " width=4\n", " )\n", ")\n", "trace1 = go.Scatter(\n", " x = month,\n", " y = high_2007,\n", " name = 'High 2007',\n", " line = dict(\n", " color='rgb(25, 120, 24)', \n", " width=4, \n", " dash='dash'\n", " # dash options include 'dash', 'dot', and 'dashdot'\n", " ) \n", ")\n", "trace2 = go.Scatter(\n", " x = month,\n", " y = high_2000,\n", " name = 'High 2000',\n", " line = dict(\n", " color='rgb(25, 12, 204)', \n", " width=4, \n", " dash='dot'\n", " )\n", ")\n", "\n", "data = [trace0, trace1, trace2]\n", "layout = dict(\n", " title = 'Average High and Low Temperatures in New York',\n", " xaxis = dict(title = 'Month'),\n", " yaxis = dict(title = 'Temperature (degrees F)'),\n", ")\n", "fig = dict(data=data, layout=layout)\n", "plotly.offline.iplot(fig, filename='styled-line')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interpolation with Line Plots" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "linkText": "Export to plot.ly", "plotlyServerURL": "https://plot.ly", "showLink": true }, "data": [ { "hoverinfo": "name", "line": { "shape": "linear" }, "mode": "lines+markers", "name": "'linear'", "type": "scatter", "uid": "a8d14474-e3f8-44dd-9bc5-6cc53883c5dd", "x": [ 1, 2, 3, 4, 5 ], "y": [ 1, 3, 2, 3, 1 ] }, { "hoverinfo": "text+name", "line": { "shape": "spline" }, "mode": "lines+markers", "name": "'spline'", "text": [ "tweak line smoothness