Given the success of Plotly Dash and Plotly Express, it's easy to forget that Plotly's rise to success began with a product that was neither of these household names. Dash has cornered the interactive dashboard market, while Plotly Express has become the defacto Python library generating inline charts, particularly for Jupyter notebooks. What we don't get from either of these tools is a way to create data visualizations for any other scenario, such as creating a single chart to serve publicly as an image or interactive plot.
Plotly Chart Studio shines as a tool to instantly create cloud-hosted data visualizations. The syntax will appear familiar to those who have used Plotly Express (or any other Python data vis library), but Plotly Chart Studio stands alone how these charts are created: on a publicly accessible cloud. With a Chart Studio account, each chart you plot is saved to a public chart studio profile, like mine:
Each chart in my profile is publicly accessible as your choice of an embedded plot, an image, raw HTML, etc. Here's an example chart as an iFrame (sorry dev.to fam, but I don't think iFrames are supported here):
https://plotly.com/~toddbirchard/403/
Every chart created with Plotly studio automatically generates interactive plots along with static image varieties like PNGs. This opens the door to possibilities for us to create charts on demand, like creating dumb Discord bots, for example:
This tutorial will demonstrate plot creation in Chart Studio with a fairly common real-life scenario. We'll be fetching stock price data from IEX Cloud, transforming the data in a Pandas DataFrame, and outputting a Candlestick chart. This same workflow can be applied to create any of Plotly's many chart types.
Getting Set Up
The two Plotly-related libraries we need are plotly
and chart-studio
. We'll install these along with the usual suspects for Python data analysis:
Accounts & Configuration
You can create a Plotly Chart Studio account here; get yourself set up and grab an API key. I'm also using an API key to access data from IEX, which can be substituted for whichever API credentials you happen to need:
Save your secrets in a .env file as always. If you're not sure what this means, please stop coding and Google what environment variables are. Seriously, stop DMing me that my code is "broken" because you didn't create a .env file, or take 5 minutes to grasp what that even means.
What were we talking about, again? Right, charts.
Initializing Chart Studio
chart_studio is a standalone Python library to serve the sole purpose of saving charts to Plotly Chart Studio. We need to authenticate with Plotly once upfront before we chart anything, which we can do by importing the set_credentials_file
function and passing our Plotly username and API key :
We'll come back to this later. Let's get some data.
Fetching & Preparing Data
Beautiful charts need beautiful data. We're going to get some beautiful data using two libraries you should hopefully be sick of by now: requests and Pandas.
Fetch Data from an API
Let's GET
this bread! Below is a function called fetch_stock_data()
, which constructs a GET
request to our API. In my example, we're fetching 1-month's worth of stock price data for a given stock symbol:
This is a straightforward GET request made to a REST API endpoint. The above effectively constructs and hits this URL:
If everything goes correctly, req.content
should spit out some raw JSON. Here's a glance at what that looks like:
This response might seem daunting at first glance, but it's exactly what we're looking for. We just need a little help from an old friend...
Parsing with Pandas
It's time for some mo effin' Pandas! We're going to take the data
we fetched from our API request and pass it into a new function called parse_data()
, which will load our data into a Pandas DataFrame. Shoutout to the beautiful new Pandas method called read_json()
:
It's important to note that we pass in req.content
from our request - not req.json()
. req.json()
renders JSON as a Python dictionary, which you could load into Pandas with from_dict()
, but whatever. Stick with me here.
Create a Chart with Plotly Studio
Let's handle this one part one step at a time. There are three things we need to do here:
- Create a Plotly chart with the data from our Pandas DataFrame.
- Adjust aspects of our chart's layout (such as title, color, etc.).
- Saving our chart to Plotly's cloud.
Each step above is nice and easy. Here's step 1 :
If you've ever used Plotly or Plotly Dash, this syntax should look familiar! go.Figure()
is a Python class used to create charts/plots/figures/whatever. We're passing data
as a keyword argument, which is actually just one of 3 notable arguments we could pass:
-
data : Accepts one or multiple "trace" types. A "trace" refers to the data on our plot; in other words, we could easily chart different types of data on the same plot, such as a combination of line chart data (
go.Line
) and bar chart data (go.Bar
) on the same plot. In our case, we're sticking with a single candlestick trace, where we pass in columns of our Pandas DataFrame for each valuego.Candlestick()
expects. - layout : A plot's layout gives us the power to change anything and everything about our chart's appearance. This could be background color, trace color, plot title, margins, and so much more. Just look at all the ways we can customize our plot - it's almost ridiculous!
-
frames : Apparently, we could animate our charts by passing multiple frames of changing data into the
frames
keyword argument. That's just fuckin bonkers imo and not something we should worry about until we're older.
Anyway, on to step 2 : customizing our layout:
We pass layout
into our figure immediately after passing data
. As we've already established, there's a shit ton of possibilities we could get deep into here. I'm keeping things modest by setting a chart title and making sure the x-axis knows it's a range of dates.
We can finally save our hard work with step 3 : saving our plot to the cloud:
The options we're passing to py.plot()
are pretty straightforward:
- Setting the filename.
- Handling file clashes by overwriting preexisting charts with the same name.
- Making the chart available to the public
So, what does chart
actually output? Here's what a print()
would reveal:
It's a link to our chart! But wait, what if I don't want to view my chart in on Plotly's site? What if I want an image? Check out what chart[:-1] + '.png'
gives you:
https://plotly.com/~toddbirchard/466.png
Which is a URL to...
WE DID IT!
Organizing Our Work
Throughout this tutorial, you've kinda just been watching me dump random code blocks one-by-one. If this confuses the shit out of you, it'll probably help to know that this is how I chose to structure my project:
"That doesn't tell me shit," you might exclaim. Luckily, that was just a set up to tell you that the source code for this tutorial is available for up on Github. Enjoy:
hackersandslackers / plotly-chartstudio-tutorial
📈 📊 Create Cloud-hosted Charts with Plotly Chart Studio.
Plotly Chart Studio Tutorial
Use Pandas and Plotly to create cloud-hosted data visualizations on demand in Python. Source for the accompanying tutorial: https://hackersandslackers.com/plotly-chart-studio/
Installation
Installation via requirements.txt
:
$ git clone https://github.com/hackersandslackers/plotly-chartstudio-tutorial.git
$ cd plotly-chartstudio-tutorial
$ python3 -m venv myenv
$ source myenv/bin/activate
$ pip3 install -r requirements.txt
$ python3 main.py
Installation via Pipenv:
$ git clone https://github.com/hackersandslackers/plotly-chartstudio-tutorial.git
$ cd plotly-chartstudio-tutorial
$ pipenv shell
$ pipenv update
$ python3 main.py
Installation via Poetry:
$ git clone https://github.com/hackersandslackers/plotly-chartstudio-tutorial.git
$ cd plotly-chartstudio-tutorial
$ poetry shell
$ poetry update
$ poetry run
Hackers and Slackers tutorials are free of charge. If you found this tutorial helpful, a small donation would be greatly appreciated to keep us in business. All proceeds go towards coffee, and all coffee goes towards more content.
Top comments (0)