The Lines
object provides the following features:
colors
, line_style
, stroke_width
etc.markers
attributeThe 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 |
To explore more features, run the following lines of code:
from bqplot import Lines
?Lines
or visit the Lines
documentation page
Let's explore these features one by one
We begin by importing the modules that we will need in this example
In [ ]:
import numpy as np #For numerical programming and multi-dimensional arrays
from pandas import date_range #For date-rate generation
from bqplot import LinearScale, Lines, Axis, Figure, DateScale, ColorScale
Random Data Generation
In [ ]:
security_1 = np.cumsum(np.random.randn(150)) + 100.
security_2 = np.cumsum(np.random.randn(150)) + 100.
Using the bqplot
, object oriented API, we can generate a Line Chart with the following code snippet:
In [ ]:
sc_x = LinearScale()
sc_y = LinearScale()
line = Lines(x=np.arange(len(security_1)), y=security_1,
scales={'x': sc_x, 'y': sc_y})
ax_x = Axis(scale=sc_x, label='Index')
ax_y = Axis(scale=sc_y, orientation='vertical', label='y-values of Security 1')
Figure(marks=[line], axes=[ax_x, ax_y], title='Security 1')
The x
attribute refers to the data represented horizontally, while the y
attribute refers the data represented vertically.
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.
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 [ ]:
dt_x = DateScale()
sc_y = LinearScale()
time_series = Lines(x=dates, y=security_1, scales={'x': dt_x, 'y': sc_y})
ax_x = Axis(scale=dt_x, label='Date')
ax_y = Axis(scale=sc_y, orientation='vertical', label='Security 1')
Figure(marks=[time_series], axes=[ax_x, ax_y], title='A Time Series Plot')
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 [ ]:
x_dt = DateScale()
y_sc = LinearScale()
We pass each data set as an element of a list
. The colors attribute allows us to pass a specific color for each line.
In [ ]:
dates_new = date_range(start='06-01-2007', periods=150)
In [ ]:
securities = np.cumsum(np.random.randn(150, 10), axis=0)
positions = np.random.randint(0, 2, size=10)
In [ ]:
# We pass the color scale and the color data to the lines
line = Lines(x=dates, y=[security_1, security_2],
scales={'x': x_dt, 'y': y_sc},
labels=['Security 1', 'Security 2'])
In [ ]:
ax_x = Axis(scale=x_dt, label='Date')
ax_y = Axis(scale=y_sc, orientation='vertical', label='Security 1')
Figure(marks=[line], axes=[ax_x, ax_y], legend_location='top-left')
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]
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 [ ]:
x_dt = DateScale()
y_sc = LinearScale()
col_sc = 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)
positions = np.random.randint(0, 2, size=10)
# Here we generate 10 random price series and 10 random positions
In [ ]:
# We pass the color scale and the color data to the lines
line = Lines(x=dates_color, y=securities.T,
scales={'x': x_dt, 'y': y_sc, 'color': col_sc}, color=positions,
labels=['Security 1', 'Security 2'])
In [ ]:
ax_x = Axis(scale=x_dt, label='Date')
ax_y = Axis(scale=y_sc, orientation='vertical', label='Security 1')
Figure(marks=[line], axes=[ax_x, ax_y], legend_location='top-left')
We can also reset the colors of the Line to their defaults by setting the color
attribute to None
.
In [ ]:
line.color = None
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 [ ]:
sc_x = LinearScale()
sc_y = LinearScale()
patch = Lines(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]],
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]],
fill_colors=['orange', 'blue', 'red'],
fill='inside',
stroke_width=10,
close_path=True,
scales={'x': sc_x, 'y': sc_y},
display_legend=True)
Figure(marks=[patch], animation_duration=1000)
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