user1610719
user1610719

Reputation: 1303

Include labels for each data point in pandas plotting

Assume we have a DataFrame with prices and volume(think finance).

What's the best way to label each price point with the volume of that price point?

                  Price   Volume
2013-04-10 04:46  1300      19
2013-04-10 04:47  1305      20
2013-04-10 04:48  1302       6
2013-04-10 04:49  1301      10

Upvotes: 2

Views: 6861

Answers (1)

spencerlyon2
spencerlyon2

Reputation: 9676

Here is one possible implementation

I have import the following:

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

Now we can recreate the data

ind = pd.date_range(start=dt.datetime(2013, 4, 10, 4, 46),
                      periods=4, freq='Min')

data = pd.DataFrame([[1200, 19], [1302, 20], [1302, 6], [1301, 10]],
                    index=ind, columns=['Price', 'Volume'])

Now I will define the annotate_plot funciton. The docstrings should have enough information to figure out what it is doing.

def annotate_plot(frame, plot_col, label_col, **kwargs):
    """
    Annotate the plot of a given DataFrame using one of its columns

    Should be called right after a DataFrame or series plot method,
    before telling matplotlib to show the plot.

    Parameters
    ----------
    frame : pandas.DataFrame

    plot_col : str
        The string identifying the column of frame that was plotted

    label_col : str
        The string identifying the column of frame to be used as label

    kwargs:
        Other key-word args that should be passed to plt.annotate

    Returns
    -------
    None

    Notes
    -----
    After calling this function you should call plt.show() to get the
    results. This function only adds the annotations, it doesn't show
    them.
    """
    import matplotlib.pyplot as plt  # Make sure we have pyplot as plt

    for label, x, y in zip(frame[label_col], frame.index, frame[plot_col]):
        plt.annotate(label, xy=(x, y), **kwargs)

This function can now be used to do a basic plot with labels

data.Price.plot(marker='*')
annotate_plot(data, 'Price', 'Volume')
plt.show()

You can also pass arbitrary arguments through the annotate_plot function that go directly to plt.annotate(). Note that most of these arguments were taken from this answer.

bbox = dict(boxstyle='round,pad=0.5', fc='green', alpha=0.3)
ha = 'right'
va = 'bottom'
arrowprops = dict(arrowstyle='->', connectionstyle='arc3,rad=0')
xytext = (-20, 20)
textcoords = 'offset points'

data.Price.plot(marker='*')
annotate_plot(data, 'Price', 'Volume', bbox=bbox, ha=ha, va=va, 
              xytext=xytext, textcoords=textcoords)

plt.show()

Upvotes: 3

Related Questions