Skip to content

Commit

Permalink
add dash plot_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
divyachandran-ds committed Mar 25, 2019
1 parent ff497b3 commit 2ddebf6
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 66 deletions.
146 changes: 101 additions & 45 deletions _posts/python/databases/plot-csv/2015-06-30-plotting-csv-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
name: Plot CSV Data
has_thumbnail: true
thumbnail: thumbnail/csv.jpg
layout: user-guide
language: python
title: Plot Data from CSV | plotly
display_as: databases
has_thumbnail: true
language: python
page_type: example_index
has_thumbnail: false
display_as: databases
order: 1
ipynb: ~notebook_demo/84
layout: user-guide
---
{% raw %}
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="New-to-Plotly?">New to Plotly?<a class="anchor-link" href="#New-to-Plotly?">&#182;</a></h4><p>Plotly's Python library is free and open source! <a href="https://plot.ly/python/getting-started/">Get started</a> by downloading the client and <a href="https://plot.ly/python/getting-started/">reading the primer</a>.
<br>You can set up Plotly to work in <a href="https://plot.ly/python/getting-started/#initialization-for-online-plotting">online</a> or <a href="https://plot.ly/python/getting-started/#initialization-for-offline-plotting">offline</a> mode, or in <a href="https://plot.ly/python/getting-started/#start-plotting-online">jupyter notebooks</a>.
Expand All @@ -32,11 +30,11 @@ <h4 id="Version-Check">Version Check<a class="anchor-link" href="#Version-Check"
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">plotly</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">plotly</span>
<span class="n">plotly</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>

</div>
</div>
</div>
</div>

Expand All @@ -45,7 +43,9 @@ <h4 id="Version-Check">Version Check<a class="anchor-link" href="#Version-Check"


<div class="output_area">
<div class="prompt output_prompt">Out[1]:</div>

<div class="prompt output_prompt">Out[1]:</div>




Expand All @@ -59,10 +59,8 @@ <h4 id="Version-Check">Version Check<a class="anchor-link" href="#Version-Check"
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#182;</a></h4>
</div>
Expand All @@ -73,23 +71,21 @@ <h4 id="Imports">Imports<a class="anchor-link" href="#Imports">&#182;</a></h4>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">plotly.plotly</span> <span class="kn">as</span> <span class="nn">py</span>
<span class="kn">import</span> <span class="nn">plotly.graph_objs</span> <span class="kn">as</span> <span class="nn">go</span>
<span class="kn">import</span> <span class="nn">plotly.figure_factory</span> <span class="kn">as</span> <span class="nn">FF</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">plotly.plotly</span> <span class="k">as</span> <span class="nn">py</span>
<span class="kn">import</span> <span class="nn">plotly.graph_objs</span> <span class="k">as</span> <span class="nn">go</span>
<span class="kn">import</span> <span class="nn">plotly.figure_factory</span> <span class="k">as</span> <span class="nn">FF</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
</pre></div>

</div>
</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple-Example">&#182;</a></h4><p>CSV or comma-delimited-values is a very popular format for storing structured data. In this tutorial, we will see how to plot beautiful graphs using csv data, and Pandas. We will import data from a local file <code>sample-data.csv</code> with the pandas function: <code>read_csv()</code>.</p>

Expand All @@ -101,13 +97,13 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;sample-data.csv&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;sample-data.csv&#39;</span><span class="p">)</span>

<span class="n">sample_data_table</span> <span class="o">=</span> <span class="n">FF</span><span class="o">.</span><span class="n">create_table</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">())</span>
<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">sample_data_table</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;sample-data-table&#39;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>
</div>

Expand All @@ -116,7 +112,9 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple


<div class="output_area">
<div class="prompt output_prompt">Out[3]:</div>

<div class="prompt output_prompt">Out[3]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
Expand All @@ -134,7 +132,7 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">trace1</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">trace1</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span>
<span class="n">x</span><span class="o">=</span><span class="n">df</span><span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">],</span> <span class="n">y</span><span class="o">=</span><span class="n">df</span><span class="p">[</span><span class="s1">&#39;logx&#39;</span><span class="p">],</span> <span class="c1"># Data</span>
<span class="n">mode</span><span class="o">=</span><span class="s1">&#39;lines&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;logx&#39;</span> <span class="c1"># Additional options</span>
<span class="p">)</span>
Expand All @@ -150,7 +148,7 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple
<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;simple-plot-from-csv&#39;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>
</div>

Expand All @@ -159,7 +157,9 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple


<div class="output_area">
<div class="prompt output_prompt">Out[4]:</div>

<div class="prompt output_prompt">Out[4]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
Expand All @@ -172,10 +172,8 @@ <h4 id="A-Simple-Example">A Simple Example<a class="anchor-link" href="#A-Simple
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a class="anchor-link" href="#Plotting-Data-from-External-Source">&#182;</a></h4><p>In the next example, we will learn how to import csv data from an external source (a url), and plot it using Plotly and pandas. We are going to use this data for the example.</p>

Expand All @@ -187,13 +185,13 @@ <h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;https://raw.githubusercontent.com/plotly/datasets/master/2014_apple_stock.csv&#39;</span><span class="p">)</span>

<span class="n">df_external_source</span> <span class="o">=</span> <span class="n">FF</span><span class="o">.</span><span class="n">create_table</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">())</span>
<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">df_external_source</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;df-external-source-table&#39;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>
</div>

Expand All @@ -202,7 +200,9 @@ <h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a


<div class="output_area">
<div class="prompt output_prompt">Out[5]:</div>

<div class="prompt output_prompt">Out[5]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
Expand All @@ -220,17 +220,17 @@ <h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">trace</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span><span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;AAPL_x&#39;</span><span class="p">],</span> <span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;AAPL_y&#39;</span><span class="p">],</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">trace</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Scatter</span><span class="p">(</span><span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;AAPL_x&#39;</span><span class="p">],</span> <span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;AAPL_y&#39;</span><span class="p">],</span>
<span class="n">name</span><span class="o">=</span><span class="s1">&#39;Share Prices (in USD)&#39;</span><span class="p">)</span>
<span class="n">layout</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Layout</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s1">&#39;Apple Share Prices over time (2014)&#39;</span><span class="p">,</span>
<span class="n">plot_bgcolor</span><span class="o">=</span><span class="s1">&#39;rgb(230, 230,230)&#39;</span><span class="p">,</span>
<span class="n">showlegend</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">showlegend</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">fig</span> <span class="o">=</span> <span class="n">go</span><span class="o">.</span><span class="n">Figure</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">[</span><span class="n">trace</span><span class="p">],</span> <span class="n">layout</span><span class="o">=</span><span class="n">layout</span><span class="p">)</span>

<span class="n">py</span><span class="o">.</span><span class="n">iplot</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;apple-stock-prices&#39;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>
</div>

Expand All @@ -239,7 +239,9 @@ <h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a


<div class="output_area">
<div class="prompt output_prompt">Out[6]:</div>

<div class="prompt output_prompt">Out[6]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">
Expand All @@ -252,12 +254,66 @@ <h4 id="Plotting-Data-from-External-Source">Plotting Data from External Source<a
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Dash-Example">Dash Example<a class="anchor-link" href="#Dash-Example">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.display</span> <span class="k">import</span> <span class="n">IFrame</span>
<span class="n">IFrame</span><span class="p">(</span><span class="n">src</span><span class="o">=</span> <span class="s2">&quot;https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/&quot;</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="s2">&quot;100%&quot;</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="s2">&quot;650px&quot;</span><span class="p">,</span> <span class="n">frameBorder</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[1]:</div>



<div class="output_html rendered_html output_subarea output_execute_result">

<iframe
width="100%"
height="650px"
src="https://dash-simple-apps.plotly.host/dash-plotfromcsvplot/?frameBorder=0"
frameborder="0"
allowfullscreen
></iframe>

</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="https://github.com/plotly/simple-example-chart-apps/tree/master/plot_from_csv">source code</a></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Reference">Reference<a class="anchor-link" href="#Reference">&#182;</a></h4><p>See <a href="https://plot.ly/python/getting-started">https://plot.ly/python/getting-started</a> for more information about Plotly's Python Open Source Graphing Library!</p>
<h4 id="Reference">Reference<a class="anchor-link" href="#Reference">&#182;</a></h4><p>See <a href="https://plot.ly/python/getting-started">https://plot.ly/python/getting-started</a> for more information about Plotly's Python API!</p>

</div>
</div>
Expand Down
Loading

0 comments on commit 2ddebf6

Please sign in to comment.