Introduction

The Lines object provides the following features:

  1. Ability to plot a single set or multiple sets of y-values as a function of a set or multiple sets of x-values
  2. Ability to style the line object in different ways, by setting different attributes such as the colors, line_style, stroke_width etc.
  3. Ability to specify a marker at each point passed to the line. The marker can be a shape which is at the data points between which the line is interpolated and can be set through the markers attribute

The Lines object has the following attributes

Attribute Description Default Value
colors Sets the color of each line, takes as input a list of any RGB, HEX, or HTML color name CATEGORY10
opacities Controls the opacity of each line, takes as input a real number between 0 and 1 1.0
stroke_width Real number which sets the width of all paths 2.0
line_style Specifies whether a line is solid, dashed, dotted or both dashed and dotted 'solid'
interpolation Sets the type of interpolation between two points 'linear'
marker Specifies the shape of the marker inserted at each data point None
marker_size Controls the size of the marker, takes as input a non-negative integer 64
close_path Controls whether to close the paths or not False
fill Specifies in which way the paths are filled. Can be set to one of {'none', 'bottom', 'top', 'inside'} None
fill_colors List that specifies the fill colors of each path []
Data Attribute Description Default Value
x abscissas of the data points array([])
y ordinates of the data points array([])
color Data according to which the Lines will be colored. Setting it to None defaults the choice of colors to the colors attribute None

pyplot's plot method can be used to plot lines with meaningful defaults


In [ ]:
import numpy as np
from pandas import date_range
import bqplot.pyplot as plt
from bqplot import ColorScale

In [ ]:
security_1 = np.cumsum(np.random.randn(150)) + 100.
security_2 = np.cumsum(np.random.randn(150)) + 100.

Basic Line Chart


In [ ]:
fig = plt.figure(title='Security 1')
axes_options = {'x': {'label': 'Index'}, 'y': {'label': 'Price'}}
# x values default to range of values when not specified
line = plt.plot(security_1, axes_options=axes_options)
fig

We can explore the different attributes by changing each of them for the plot above:


In [ ]:
line.colors = ['DarkOrange']

In a similar way, we can also change any attribute after the plot has been displayed to change the plot. Run each of the cells below, and try changing the attributes to explore the different features and how they affect the plot.


In [ ]:
# The opacity allows us to display the Line while featuring other Marks that may be on the Figure
line.opacities = [.5]

In [ ]:
line.stroke_width = 2.5

To switch to an area chart, set the fill attribute, and control the look with fill_opacities and fill_colors.


In [ ]:
line.fill = 'bottom'
line.fill_opacities = [0.2]

In [ ]:
line.line_style = 'dashed'

In [ ]:
line.interpolation = 'basis'

While a Lines plot allows the user to extract the general shape of the data being plotted, there may be a need to visualize discrete data points along with this shape. This is where the markers attribute comes in.


In [ ]:
line.marker = 'triangle-down'

The marker attributes accepts the values square, circle, cross, diamond, square, triangle-down, triangle-up, arrow, rectangle, ellipse. Try changing the string above and re-running the cell to see how each marker type looks.

Plotting a Time-Series

The DateScale allows us to plot time series as a Lines plot conveniently with most date formats.


In [ ]:
# Here we define the dates we would like to use
dates = date_range(start='01-01-2007', periods=150)

In [ ]:
fig = plt.figure(title='Time Series')
axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Security 1'}}
time_series = plt.plot(dates, security_1, 
                       axes_options=axes_options)
fig

Plotting multiples sets of data

The Lines mark allows the user to plot multiple y-values for a single x-value. This can be done by passing an ndarray or a list of the different y-values as the y-attribute of the Lines as shown below.


In [ ]:
dates_new = date_range(start='06-01-2007', periods=150)

We pass each data set as an element of a list


In [ ]:
fig = plt.figure()
axes_options = {'x': {'label': 'Date'}, 'y': {'label': 'Price'}}
line = plt.plot(dates, [security_1, security_2], 
                labels=['Security 1', 'Security 2'],
                axes_options=axes_options,
                display_legend=True)
fig

Similarly, we can also pass multiple x-values for multiple sets of y-values


In [ ]:
line.x, line.y = [dates, dates_new], [security_1, security_2]

Coloring Lines according to data

The color attribute of a Lines mark can also be used to encode one more dimension of data. Suppose we have a portfolio of securities and we would like to color them based on whether we have bought or sold them. We can use the color attribute to encode this information.


In [ ]:
fig = plt.figure()
axes_options = {'x': {'label': 'Date'}, 
                'y': {'label': 'Security 1'},
                'color' : {'visible': False}}
# add a custom color scale to color the lines
plt.scales(scales={'color': ColorScale(colors=['Red', 'Green'])})

In [ ]:
dates_color = date_range(start='06-01-2007', periods=150)

In [ ]:
securities = 100. + np.cumsum(np.random.randn(150, 10), axis=0)
# we generate 10 random price series and 10 random positions
positions = np.random.randint(0, 2, size=10)

In [ ]:
# We pass the color scale and the color data to the plot method
line = plt.plot(dates_color, securities.T, color=positions, 
                axes_options=axes_options)
fig

We can also reset the colors of the Line to their defaults by setting the color attribute to None.


In [ ]:
line.color = None

Patches

The fill attribute of the Lines mark allows us to fill a path in different ways, while the fill_colors attribute lets us control the color of the fill


In [ ]:
fig = plt.figure(animation_duration=1000)
patch = plt.plot([],[],
                 fill_colors=['orange', 'blue', 'red'],
                 fill='inside',
                 axes_options={'x': {'visible': False}, 'y': {'visible': False}},
                 stroke_width=10,
                 close_path=True,
                 display_legend=True)

patch.x = [[0, 2, 1.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7 , np.nan, np.nan, np.nan, np.nan], [4, 5, 6, 6, 5, 4, 3]], 
patch.y = [[0, 0, 1  , np.nan, np.nan, np.nan, np.nan], [0.5, 0.5, -0.5, np.nan, np.nan, np.nan, np.nan], [1, 1.1, 1.2, 2.3, 2.2, 2.7, 1.0]]
fig

In [ ]:
patch.opacities = [0.1, 0.2]

In [ ]:
patch.x = [[2, 3, 3.2, np.nan, np.nan, np.nan, np.nan], [0.5, 2.5, 1.7, np.nan, np.nan, np.nan, np.nan], [4,5,6, 6, 5, 4, 3]]

In [ ]:
patch.close_path = False