<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikita Khutorni on Medium]]></title>
        <description><![CDATA[Stories by Nikita Khutorni on Medium]]></description>
        <link>https://medium.com/@nkhutorni?source=rss-a09a54bc2b5d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*s8nDpBrLvsOxoGn4MOnjSw.png</url>
            <title>Stories by Nikita Khutorni on Medium</title>
            <link>https://medium.com/@nkhutorni?source=rss-a09a54bc2b5d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:05:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nkhutorni/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to make multi-index index charts with Plotly]]></title>
            <link>https://medium.com/data-science/how-to-make-multi-index-index-charts-with-plotly-4d3984cd7b09?source=rss-a09a54bc2b5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d3984cd7b09</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[plotly]]></category>
            <dc:creator><![CDATA[Nikita Khutorni]]></dc:creator>
            <pubDate>Fri, 29 Jan 2021 14:05:36 GMT</pubDate>
            <atom:updated>2025-08-27T21:53:59.551Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<h4>Index charts with more insights</h4><p><strong>Note</strong>: You can read the article as a Jupyter Notebook on <a href="https://github.com/nikitakhutorni/multi-index-chart">GitHub</a>.</p><p>When working with time series data, raw numbers often provide little insight. If you have multiple variables that vary in magnitude, changes in smaller values are hard to see. There is a simple solution to this problem: Using <strong>index charts</strong> that index the data.</p><p>While regular index charts work fine, by using a simple extension to indexing you can significantly improve your visual presentation and help your charts communicate causalities in the data. This extension is what I refer to as “<strong>multi-indexing</strong>” — using multiple indices instead of one. That allows you to analyze data from different vantage points. It is especially useful when you suspect that there were events at specific points in time that had a significant influence on the growth of variables in question.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*zE6sUM13Oxh5VAEcPp6fqg.png" /><figcaption>Events with a big impact on stock market prices (Image by Author)</figcaption></figure><p>As an example, if you were to compare stock prices for some stocks from 1980 to today, you would find that the <a href="https://en.wikipedia.org/wiki/Black_Monday_(1987)">Black Monday in 1987</a>, the <a href="https://en.wikipedia.org/wiki/Dot-com_bubble">Dot-com bubble in 2000</a>, and the <a href="https://en.wikipedia.org/wiki/Financial_crisis_of_2007%E2%80%932008">stock market crash in 2008</a> were all events that influenced the market heavily and would be interesting to look at. So, you would add them to the indices list, reference data to each of these dates and compare how the prices developed with respect to each reference point. Looking at the prices this way would give you a better intuition of the magnitude of each event compared to just a single index date.</p><p>In this article I will teach you how you can implement multi-indexing in time-series plots using the Python Plotly data visualization library. I will present three approaches with complete code and explain them in detail, including the advantages and disadvantages of each approach and when to use which. To keep up with the article, you should be familiar with Python and Pandas and preferably have had some exposure to Plotly.</p><p>If you care about only one of the three charts I will talk about, feel free to skip the others — the article is written in a modular way, so you won’t miss anything. However make sure to thoroughly read part 1 (<em>Setup and Preparation</em>), because it contains important concepts and function definitions that will be used in every chart.</p><p>[You can find the entire <a href="https://github.com/nikitakhutorni/Articles/tree/main/MultiIndexChart">Jupyter Notebook of this post here.</a>]</p><p><strong><em>If you enjoyed this article, please </em></strong><a href="https://medium.com/@nkhutorni"><strong><em>follow me on Medium</em></strong></a><em>.</em></p><h3><strong>Article index</strong></h3><ol><li><a href="#29f9">Setup and preparation</a></li><li><a href="#9799">Index chart with fixed reference dates</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8MwTLATZT5vmUOpm60Io_w.gif" /><figcaption>Image by Author</figcaption></figure><p>3. <a href="#2e1b">Dynamic index chart</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TNI7elyIZOUveGbyfsIeag.gif" /><figcaption>Image by Author</figcaption></figure><p>4. <a href="#e1ec">Index chart with variable index in JupyterLab</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u947Acsi2oioX7Zf2IJD2Q.gif" /><figcaption>Image by Author</figcaption></figure><h3>1. Setup and preparation</h3><h4>1.1 Fetching and cleaning data</h4><p>As already mentioned, data preparation tasks will be done with Pandas. Most of the plotting will be handled by the Plotly graph_objects module. Additionaly for one plot we will use the ipywidgets library, which provides widgets for interacting with plots in Jupyter Notebook and JupyterLab. For the time series data we are going to use historic stock market data from Yahoo!Finance API. There is a great Python library called <a href="https://pypi.org/project/yfinance/">yfinance</a> which we will use to interface with the API.</p><p>Our goal is to build index charts that will enable the user to compare the development of selected stocks relative to a selected date on the first glance. So, let’s pick some stocks to compare. We will be featuring Facebook (FB), Amazon (AMZN), DowDupont (DD) and also Berkshire Hathaway class A shares (BRK-A) which is the highest-ever priced share for a stock. I purposefully chose stocks from a very broad price range, to demonstrate the effectiveness of indexing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86348f217225714cec416e5fbff93e0e/href">https://medium.com/media/86348f217225714cec416e5fbff93e0e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Li6qK9IGHQ1Q9f9AYkt30A.png" /><figcaption>Image by Author</figcaption></figure><p>The call to yf.download() downloads data for the given time period in given intervals, in our case starting from today and dating 3 years back, once per week. The result is stored in a multi-indexed Pandas dataframe.</p><p>Setting group_by=&#39;column&#39; specifies how the data will be grouped, either by price type (column) or tickers (ticker). This translates to whether price types or tickers will be the top-level index of our dataframe.</p><p>The table contains five different prices. To simplify things, we will only look at the adjusted closing price and drop everything else. We’ll remove empty values as well.</p><pre>df = df.filter(regex=&quot;Adj Close&quot;)<br>df = df.dropna()<br>df.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_yf0WSWpbgzX94kc7BdBrA.png" /><figcaption>Image by Author</figcaption></figure><h4>1.2 Indexing: Why And How</h4><p>Now that our data is ready, let’s take a first glance at the values.</p><pre>df.plot()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*NrlguwnxT9fHp2N1F9FREw.png" /><figcaption>Image by Author</figcaption></figure><p>Plotting raw values helps little in comparing the stocks. Due to by magnitudes higher prices of BRK-A shares, changes in all other stocks are practically invisible. Maybe scaling the data logarithmically will alleviate the problem?</p><pre>df.plot(logy=True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*7SWR7ypnKJv1VNyvrZyzFA.png" /><figcaption>Image by Author</figcaption></figure><p>Even at logarithmic scale the discrepancy in values is too big for us to meaningfully compare the stocks. It is evident that we need indexing.</p><p>Indexing is simple: Pick a date <strong>D</strong>, the index, and divide the price at every date by the value at date <strong>D</strong>. Now, all resulting values are represented as their ratio to the value <strong>V</strong> at <strong>D</strong> and can be easily compared to each other. In our case, we need to distinguish between different stocks, so for every stock we divide all values by the value at date <strong>D </strong>for the respective stock. We can do this easily in Pandas, as we have a multi-indexed dataframe. Calling iloc on a specific index returns a Pandas series containing the values of all stocks for the specific index. When we divide the dataframe by a series, Pandas divides every column by the corresponding element in the series.</p><p>We will also add some cosmetics. Multiplying the new values by 100 will give us percentage values, that are more comfortable to work with. The second optional cosmetic is subtracting 100 from the result. This effectively sets 100% to 0% and changes <em>ratio</em> to <em>relative difference</em>. As an example, a drop to 30% of the original value will now be interpreted as original value — 70 %. Personally, I think that this interpretation is easier to comprehend.</p><p>Note that I will refer to the index date as <em>reference date</em> and index value as <em>reference value</em>. This is done to avoid confusion with the dataframe index.</p><pre># use the first date as index <br>reference_value = df.iloc[0]</pre><pre># dividing by the series divides each column by the <br># corresponding element in the series<br>tmp_df = df.div(reference_value) * 100 - 100</pre><pre>tmp_df.plot()</pre><pre>prepared_df = df.copy()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*dwd-Jgkg34mqsshDPuIJiA.png" /><figcaption>Image by Author</figcaption></figure><p>This looks much better. The stock price trends are immediately visible and comparable.</p><p>We are going to use indexing many more times, so let’s write a generalized function create_indexed_columns() for this technique. The function will take the following parameters:</p><ol><li>date - the reference date that is the index</li><li>df - the dataframe to work on</li><li>top_level_name - (optional) name for the top level index of the normalized dataframe</li></ol><p>and return a new dataframe with normalized data and a new top level index.</p><p>One important consideration is how to handle an input date that is not in the dataframe. Luckily, dataframe indices have a very useful method get_loc() that will find the integer index of given index. When we pass the method=nearest parameter to the method, Pandas will search for the closest index to the provided input. As our index is of type datetime, the date that is closest to the provided date will be matched.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0adbeeb32fbe08070f6da04e8c258f66/href">https://medium.com/media/0adbeeb32fbe08070f6da04e8c258f66/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbasic-log-chart%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbasic-log-chart%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2Fef5414f2-546d-44b1-8533-d420abe1a063.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/39c972d10c1d13b88a8c11fa7298041d/href">https://medium.com/media/39c972d10c1d13b88a8c11fa7298041d/href</a></iframe><p>Before we move on to the first Plotly index chart, let’s recap the concept of <strong>“multi-index”</strong> charts. As we have seen, indexing data to one point in time is very effective. However, often it is useful to look at multiple reference points. These might be specific events in the time series that had a big influence on our data. Looking at the data from multiple points gives the user the ability to easily see how the variable in question has grown relative to <em>different</em> reference points. The best way to accomplish this is to embed this functionality directly into the chart.</p><h4>1.3 Very Short Plotly Crashcourse</h4><p>This quick introduction only briefly touches on the basic concepts of Plotly. Skip it if you have worked with Plotly before.</p><p>Having multiple indices in a plot implies that they need to be switchable on the fly, therefore our plot has to be interactive. Plotly is interactive out of the box and extending interactability is relatively simple, which is why Plotly is the library of choice.</p><p>In Plotly, every figure is a JSON-like data structure with three main properties (called <em>attributes</em>): data, layout and frames. The first two are self-explanatory while the frames attribute contains optional frames that are used in animations. You can either first write the entire figure structure using JSON or Python dictionaries and then instantiate the figure object, or create the figure object first and update figure attributes incrementally.</p><p>As a warm up, let’s recreate the default matplotlib chart that we generated by calling df.plot() in Plotly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a972b286eab0cb131e70c9f5400ccef1/href">https://medium.com/media/a972b286eab0cb131e70c9f5400ccef1/href</a></iframe><p>In the above example we instantiate the Figure object and then add individual traces to said object one by one. We can call update_layout() with a dictionary of updated attributes to change the layout of the figure.</p><p>All changes to a Plotly figure are done this way, passing a dictionary of attributes to the figure. Plotly supports “magic underscore notation” which means that we don’t need to stepwise deepen into the object to access inner attributes but rather are able to access inner attributes directly. For example, we can simply write fig.update_layout(yaxis_type=&#39;log&#39;) instead of accessing the type attribute of the yaxis attribute - in many cases a more readable approach.</p><p>Now that we are all set, we can get on to our first chart.</p><h3>2. Index chart with fixed reference dates</h3><p>For this chart, we are going to use preselected dates around the time of stock market recessions and dates that had an impact on the companies we are analyzing. The dates listed below may not be entirely accurate, but they lay approximately in the timerange of the listed events.</p><pre>ref_dict = {<br>    &quot;Cambridge Analytica scandal&quot;: &quot;2018-03-28&quot;,<br>    &quot;DowDuPont separation&quot;:        &quot;2019-04-15&quot;,<br>    &quot;Corona stock market drop&quot;:    &quot;2020-02-14&quot;<br>}</pre><p>We will index our data with respect to each of these dates and include all differently indexed data in our chart. Switching between the different indexed data will be accomplished by using a dropdown menu with an item for every reference date. Plotly refers to this as “buttons”. Clicking a button will display only the data indexed by the respective reference date. To achieve this, we need to switch the visibility of the traces we want to show to on, and switch it off for the remanining traces.</p><p>First, let’s calculate values for each reference date and populate our dataframe with the resulting data. We will create a new dataframe for each reference date and then join them into one dataframe. We will also store the closest date to the given reference date, in case that we don’t have values for that reference date.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c629f6ecb95368d08118420ea291982/href">https://medium.com/media/0c629f6ecb95368d08118420ea291982/href</a></iframe><p>Now that our table is transformed we can build our Plotly object. We will create a new figure and add a trace for each newly computed column. We are going to provide an additional meta attribute to every trace. The meta attribute can store arbitrary JSON data and has many applications. We will use it to store the reference date that a trace belongs to. This will allow us to identify a group of traces that will be switched to visible/invisible at the same time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55b57efdbfec982dbc5ca296bc93c480/href">https://medium.com/media/55b57efdbfec982dbc5ca296bc93c480/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-1%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-1%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2Ff52a014a-6854-4f61-bde4-d2cc5e3e6c3a.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2ec9f7357299d7cbd21b5c1a1c460f9/href">https://medium.com/media/d2ec9f7357299d7cbd21b5c1a1c460f9/href</a></iframe><p>That’s a lot of traces! We want to show only those that belong to one reference date. Just like before, we won’t hardcode the buttons for our exact data. It is better to generalize our procedure so that we’ll be able to handle a different set of reference dates and tickers.</p><p>To show or hide traces, we will need to set the visibility attribute of our figure. Because this is an attribute of the figure and not an individual trace, by default changes to this attribute apply to all traces. Internally, visibility is represented as an array of boolean values, where the value at a specific index represents the visibility of the trace with the same index. The trace index is the position of a trace within that array and because traces are added one by one, trace index represents the order in which the traces were added to the figure.</p><p>The visibility array allows us to target individual traces, either by passing the index of a specific trace or passing an array of boolean values where the value at each index indicates whether the corresponding trace will be shown. For example, with 4 traces, passing the array [True, False, True, False] will display only the first and third trace.</p><p>As you can imagine, relying on the order in which the traces were added to create permanent buttons is a brittle approach. If you add traces in a different order or add more traces, every mention of the visibility array would have to be checked and updated manually. This is why we added the meta attribute - this way, we don&#39;t need to rely on the order of the traces to identify which traces to switch on/off.</p><p>A simple function create_visibility_array will create the visibility array by comparing the meta value of every trace to the passed in value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b8884801c36c2295ebd4687bb9f72d1/href">https://medium.com/media/6b8884801c36c2295ebd4687bb9f72d1/href</a></iframe><p>Finally, we will write a function for generating buttons.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31026c2b7c9f67c50c570156246617c3/href">https://medium.com/media/31026c2b7c9f67c50c570156246617c3/href</a></iframe><p>Let’s clarify the button description. method specifies the Plotly method that the button calls on clicking. restyle updates the data and the layout of the plot. The args argument takes a list of chart attributes that will be updated by clicking the button, in our case the visibility attribute.</p><p>Lastly we will add the buttons to the figure and inspect the result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57c3eb6ab8428e187947fba70ce626fa/href">https://medium.com/media/57c3eb6ab8428e187947fba70ce626fa/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-2%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-2%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2F6d486663-d657-44ae-80e3-ddd8face111b.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/d70a8915fa03e11cd003857f875b811c/href">https://medium.com/media/d70a8915fa03e11cd003857f875b811c/href</a></iframe><p>Great, the buttons work as expected. The only problem is that it is somewhat hard to see the actual reference date. We can solve this by adding a vertical line at the x-coordinate of every reference date. Naturally, we’ll need to be able to toggle the visibility of these reference lines with our existing buttons.</p><p>There are two ways to do this in Plotly: The standard way is by adding a shape object to the figure. In our case we would simply add a line shape by calling figure.add_shape() and specifying the x and y coordinates. While this approach may seem straightforward, there are some difficulties specific to our case. Because shapes aren&#39;t traces, we can&#39;t change their visibility with our existing method. Rather, the visibility attribute of an individual shape object has to be set, doing which is somewhat obscure (<a href="https://stackoverflow.com/questions/42406960/accessing-shapes-parameters-in-plotly-interactive-args">this Stack Overflow question illustrates the process</a>). Furthermore, there seems to be no concise way, similar to the trace visibility array, to change the visibility of multiple shapes at once.</p><p>The second way to add reference lines is by adding a trace that will be drawn as a vertical line. To do this, two y-coordinates must be chosen that will set the boundaries of the line. However, we need to ensure that the line covers the entire plot heightwise and in addition to that, the reference line must not affect the scaling of the plot — this means that we can’t simply choose some value below and above the minimum and respectively maximum values in our dataframe as boundaries, as this would skew the scale of the remaining values. The solution is to add a secondary, invisible y-axis to the plot that will be independent of the values in the dataframe and scale the reference lines with respect to itself.</p><p>Although the secondary y-axis somewhat increases the complexity of our figure, using traces as reference lines provides a significant benefit: We can toggle the visibility of the reference lines the same way as we do with all other traces. Thus, we only need to add the reference line trace and set its meta parameter to the reference date it refers to.</p><p>As you can see, the second approach is a better fit for our situation, which is why we will utilize it. Before we add the reference lines, let’s first add the secondary y-axis to the plot. While we are at it, we might as well add some simple styling to the plot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23040efbe9a12ea00175adfc2f34373c/href">https://medium.com/media/23040efbe9a12ea00175adfc2f34373c/href</a></iframe><p>The hovermode attribute specifies how the hoverlabels will be displayed in our plot. Setting this value to &#39;x&#39; will display the values of every trace at the x-value the mouse is hovering over. We set the secondary y-axis to a fixed range. This way, the line will always extend throughout the entire height of the plot. Setting the overlaying attribute to &#39;y&#39; is very important, as this will force the traces that are defined with respect to the secondary y-axis to behave as if they were defined on the first y-axis - concretely, the combination of this attribute and fixedrange is exactly what allows us to see the reference lines yet makes their scale independent of the y-axis.</p><p>Now that the figure is prepared, we can write a create_reference_line() function that will generate the reference lines for us. One important thing to note is that as we specify two values for y-coordinates, symmetrically, we need to provide a list with two x-values as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1515380620dfcf9490a03e7dd491dc1/href">https://medium.com/media/c1515380620dfcf9490a03e7dd491dc1/href</a></iframe><p>The only additions to our previous scatter trace definitions are:</p><ul><li>mode=&quot;lines&quot; - specifies that the traces should be drawn as lines only, without marking the points in between</li><li>yaxis=&quot;y2&quot; - specifies that the secondary y-axis is to be used as the y-axis</li><li>line - determines the styling of the line</li></ul><p>Finally, the last step consists of adding the reference line traces to the plot and regenerating the visibility buttons. Done!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4ebbb2f42e3663ccc29d8139f8cb766/href">https://medium.com/media/b4ebbb2f42e3663ccc29d8139f8cb766/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-finished%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fbutton-index-chart-finished%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2F4366429b-cb86-498c-9ab2-7719e0733b1a.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/6020b26fae8d863988ad2796c21bc274/href">https://medium.com/media/6020b26fae8d863988ad2796c21bc274/href</a></iframe><h3>3. Dynamic index chart</h3><p>The second chart we will look at is a <strong>dynamic index chart</strong>. This chart allows the user to quickly move between reference dates, creating a visually appealing chart.</p><p>Because we are using only the Plotly graphing library, the best approach here is to create a frame for each reference date and then use a Plotly slider to move between them. By using a slider we can also add an animation effect for transitioning between reference dates, which will make the transitions much smoother.</p><p>First off we need to index the data to every date and create traces for it. The resulting data will be stored in a frame which is simply a figure[&#39;data&#39;] dictionary that with an identifying name. We will create the traces like in the previous example using the create_indexed_columns and create_reference_line functions. A frame will be identified by the date that the data in the frame is indexed to. The function create_traces_for_date will bundle this functionality.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e317256e7e9f72aa757ef11786fa0e68/href">https://medium.com/media/e317256e7e9f72aa757ef11786fa0e68/href</a></iframe><p>Next, we will create a slider dictionary that will define our slider. Some important attributes that we use are:</p><ul><li>yanchor and xanchor - specify where the slider will be positioned</li><li>pad - padding for the slider</li><li>currentvalue - describes how the current value of the slider will be displayed including position, font size and a prefix.</li><li>len - describes the length of the slider relative to the chart. 1 indicates that it matches the length exactly, which is what we want.</li><li>steps - is a list of steps that the slider will cycle through. We will leave it empty at first and add to it later.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/787afb684c4cbb8afd86491d1efbf269/href">https://medium.com/media/787afb684c4cbb8afd86491d1efbf269/href</a></iframe><p>The next thing to do is to create steps that we will add to our slider. A step is described by the attributes label, method and args. method specifies which Plotly function will be called when the slider value changes. By using animate we will get a smooth transition between frames. The args attribute describes the data that will be passed to the Plotly function specifed in method, consisting of a list of frames and a dictionary specifying how to transition between them. You can actually specify multiple frames for each slider step that will all be cycled through when the slider is moved to that step. In our case, we only want one frame and identify it by its name. After that we describe the transitioning properties:</p><ul><li>frame - duration specifies the total time the frame will be displayed, redraw whether the chart should be redrawn after each step.</li><li>mode - specifies how a new call to the animate function interacts with current animations. Setting it to &quot;next&quot; means that the current running frame will be finished before switching to the next one.</li><li>transition - specifies the duration of the transitioning part of the total frame duration and the <a href="https://easings.net/">easing function</a> that is used.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b5ed57ede8c20b1dba25220807074e5/href">https://medium.com/media/2b5ed57ede8c20b1dba25220807074e5/href</a></iframe><p>There are a few important details to note here. First, setting redraw=False provides significant performance benefits in our case, as we have hundreds of frames, each with hundreds of points. Without that setting, the chart updates visibly lag behind, as you will see soon. However, there are some drawbacks to disabling redraw: For example, a rangeslider which allows you to set the view range will not be updated if redraw is deactivated. Another important drawback is that the range of the y-axis will not be automatically updated, which sometimes may not show all points on the chart. In the third part of this article we will see how to combine the best of both implementations, that is, fast updates <strong>and</strong> redrawing of the chart, at the cost of some dynamicity and requiring a Jupyter notebook. There is also a way to have it all (for a reasonable amount of data points) without compromising, but this solution requires the Dash server which is beyond the scope of this article.</p><p>The other thing to remember is that the transition duration time is a part of the frame duration time, so setting it larger than the frame duration won’t change the total duration.</p><p>Furthermore, Plotly will animate transitions between multiple frames during a slider step <em>and</em> the transition between frames of two neighbouring slider steps.</p><p>Now, with all functions set, we can actually create frames and steps and populate our figure. For every date, we will create a frame with traces indexed to that date and a slider step for that frame. Note that we set the name of a frame to a iso-formatted string of the date it represents and immediately pass that string to our create_slider_step function. This is done because the slider uses the frame name to determine which frame to display.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffa878e5b74fd2956581798ad0a2add4/href">https://medium.com/media/ffa878e5b74fd2956581798ad0a2add4/href</a></iframe><p>Lastly, we build our figure. We simply use the first frame as the data attribute of our figure. We use the style_plot function to configure the secondary y-axis, and we are finished!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44765ab5f58942cc238b720651ebeb58/href">https://medium.com/media/44765ab5f58942cc238b720651ebeb58/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fdynamic-index-chart-fast%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fdynamic-index-chart-fast%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2Fe695059d-933e-40dd-a6c1-b3222e4f144d.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/38d7347ab994f98e833c6d7bfcb5dd9d/href">https://medium.com/media/38d7347ab994f98e833c6d7bfcb5dd9d/href</a></iframe><p>I have warned you about enabling redraw, but let&#39;s actually see it in action. We will create the exact same index chart as before, but this time with redraw set to true.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fdynamic-index-chart-slow%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fnkhutorni%2Freports%2Fdynamic-index-chart-slow%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fpublic%2Fe3e09d67-0a90-42fd-907b-a2ac34d152d3.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="800" height="625" frameborder="0" scrolling="no"><a href="https://medium.com/media/d382839d517ebac73c77c88c0431e1c3/href">https://medium.com/media/d382839d517ebac73c77c88c0431e1c3/href</a></iframe><p>As you see, the chart is significantly slower than before. Personally, I find this amount of lag barely acceptable; however this is the limit: if you increase the amount of data points to above 5000, the chart will be to slow for anyone to enjoy using it. To keep the speed acceptable, you need to keep the amount of data points below this number.</p><h3>4. Index chart with variable index in JupyterLab</h3><p>The third chart will allow a user to select a custom reference date. If you plan on presenting your work in a JupyterLab, don’t need fancy animations like in the dynamic index chart, yet still want to enbale the user to pick any date as the reference date, this chart is right for you. <strong>Note that this chart will work only in JupyterLab and not in Jupyter Notebooks!</strong> The chart will be implemented with the ipywidgets library that uses widgets. According to the official docs, widgets are</p><blockquote><em>eventful Python objects that have a representation in the browser, often as a controller like a slider, textbox, etc. [..] You can use widgets to build interactive GUIs for your notebooks. You can also use widgets to synchronize stateful and stateless information between Python and JavaScript.</em></blockquote><p>(<a href="https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Basics.html">Source</a>) The idea is to use widgets to provide input for our chart via GUI and to update the chart. This way, after a different reference date was selected, the chart data will be updated through a Jupyter callback and not a Plotly event.</p><p>We will build two version of the chart, using two different widgets. One will be a DatePicker widget and the second will be a SelectionSlider widget. The second version will be a robust implementation of the dynamic index chart from part 2.</p><p>First off we copy our initial dataframe and import additional libraries. Ipython.display is used to render the widgets in the browser.</p><pre>from IPython.display import display<br>from ipywidgets import widgets as wg</pre><pre>dfs = df.copy()</pre><p>Next, we create the chart object. Because we want our Plotly chart to be compatible with Jupyter widgets, our chart needs to be a FigureWidget object rather than a Figure object.</p><p>The style function we defined before works on FigureWidget objects as well, so we can simply apply it to our plot.</p><p>Before we can add interactivity, we need to populate our chart with initial data. We need to provide an initial state for our chart. In our case, state represents the selected reference date. We’ll use the starting date as our initial state, index the data to with respect to that date with create_indexed_columns and add all traces including the black reference line to the plot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d27d3db3a6082e3c4b6b9874ad3b81dc/href">https://medium.com/media/d27d3db3a6082e3c4b6b9874ad3b81dc/href</a></iframe><p>We have the base, our initial state. The next step is to write the update_chart function, that will receive a date as an input and property of the widget, normalize our data to that date using create_indexed_columns and update the y-values of the traces with these new values. To update mulitple values at once, we will perform the updates using the <a href="https://www.kite.com/python/docs/plotly.basedatatypes.BaseFigure.batch_update">figure.batch_update()</a> context manager. Every change to the layout and data inside the with figure.batch_update() block will be sent to the chart in one message, instead of multiple messages like with update and thus prevent stuttering.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db2ffef28c4cedfe2f1f4d03ccd816a7/href">https://medium.com/media/db2ffef28c4cedfe2f1f4d03ccd816a7/href</a></iframe><p>Now we are set to implement both the date picker variant and the slider variant. We’ll start off with the first.</p><p>There are four steps left:</p><ol><li>Create a DatePicker widget.</li><li>Define a callback function picker_response that listens to changes in the widget and calls update_chart.</li><li>Connect the callback function to the widget.</li><li>Put the widgets into containers and display everything.</li></ol><p>Step 1 is straightforward.</p><p>For step 2 and 3, a special handler function picker_response with the signature handler(change) must be used for the callback. change is a dictionary that holds the information about the change, such as the old and new values and the names of the widget&#39;s changed attributes. We&#39;ll use the new key to get the new value that was set on change. To bind the handler to the widget we use the observe method of the widget. By setting the names parameter of observe we specify on which attributes of the widget the callback function will be executed. In our case we need to set names=&#39;value&#39; because we want to pass the value of the slider, i.e. the selected date to the handler.</p><p>In step 4, we simply call the ipython.display function with a vertical box widget, that contains the date picker widget (wrapped in a horizontal box) and the figure widget. The boxes act like containers, similar to HTML divs. And that&#39;s it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f82fdbc3ada9221eb2245faeeec11c72/href">https://medium.com/media/f82fdbc3ada9221eb2245faeeec11c72/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u947Acsi2oioX7Zf2IJD2Q.gif" /><figcaption>Image by Author</figcaption></figure><p>The chart works as expected.</p><p>Now let’s reimplement the dynamic index chart with the slider using a slider widget. The procedure here is structurally identical to the previous chart. First, we define a selection slider widget, that stores a list of option values. We will use the dates in the index transformed to strings for our options. We use strings, because otherwise the current date will be formatted with zeroes for hours, which isn’t pretty.</p><p>Here is the part that will allow us to make this chart a robust alternative for the dynamic index chart: By setting the continuous_update attribute of the widget to false, the callback function will be executed only on release events. This will produce a chart that is not as fancy as the dynamic index chart, but with the same date selection interface and all the benefits of redrawing on every date slider change. If you enable continuous_update, the chart will behave exactly like the dynamic index chart, with the same (if not worse) speed.</p><p>Next, we write the callback function and connect it to the slider with observe. Finally, we wrap everything into a VBox widget and call display. Et voilà - the chart is ready!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b61dfdf42f2970afc15a339f871c799/href">https://medium.com/media/9b61dfdf42f2970afc15a339f871c799/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rXRgTFGu_UJFXdyVpEs2cg.gif" /><figcaption>Image by Author</figcaption></figure><p>I hope this article was of good use to you! It took me a while to find out how to implement multi-index charts, and I wanted to spare you the work.</p><p>I’d like to ask you one thing: <strong>If you enjoyed this article, please </strong><a href="https://medium.com/@nkhutorni"><strong>follow me on Medium</strong></a>. Your subscription will make Medium give me money and I like money. Thanks 😊</p><p>You can find the entire article with source code on <a href="https://github.com/nikitakhutorni/multi-index-chart">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d3984cd7b09" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-make-multi-index-index-charts-with-plotly-4d3984cd7b09">How to make multi-index index charts with Plotly</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>