Debian Codegraph @

bqplot/examples/Marks/Object Model/Label.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "from bqplot import Label, LinearScale, Axis, Lines, Figure, DateScale\n",
10 "import numpy as np\n",
11 "import pandas as pd"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": null,
17 "metadata": {},
18 "outputs": [],
19 "source": [
20 "np.random.seed(10)\n",
21 "price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,\n",
22 " columns=['Security 1', 'Security 2'],\n",
23 " index=pd.date_range(start='01-01-2007', periods=150))\n",
24 "y_data = np.cumsum(np.random.randn(100))"
25 ]
26 },
27 {
28 "cell_type": "markdown",
29 "metadata": {},
30 "source": [
31 "## Label positioned in data coordinates"
32 ]
33 },
34 {
35 "cell_type": "code",
36 "execution_count": null,
37 "metadata": {},
38 "outputs": [],
39 "source": [
40 "x_sc = LinearScale()\n",
41 "y_sc = LinearScale()\n",
42 "\n",
43 "test_line = Lines(x=np.arange(10), y=y_data[:10], scales={'x': x_sc, 'y': y_sc})\n",
44 "test_label = Label(x=np.arange(5), y=y_data[:5], scales={'x': x_sc, 'y': y_sc},\n",
45 " text=['Test', 'Label', 'for', 'the', 'Data'], default_size=26, font_weight='bolder',\n",
46 " colors=['orange', 'red'], update_on_move=True)\n",
47 "\n",
48 "ax_x = Axis(scale=x_sc, label='X')\n",
49 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f', label='Y')\n",
50 "\n",
51 "Figure(marks=[test_label, test_line], axes=[ax_x, ax_y], title='Basic Label Example')"
52 ]
53 },
54 {
55 "cell_type": "markdown",
56 "metadata": {},
57 "source": [
58 "Setting the label attribute `enable_move` to `True` makes the label draggable"
59 ]
60 },
61 {
62 "cell_type": "code",
63 "execution_count": null,
64 "metadata": {},
65 "outputs": [],
66 "source": [
67 "test_label.enable_move = True"
68 ]
69 },
70 {
71 "cell_type": "markdown",
72 "metadata": {},
73 "source": [
74 "## Label positioned in terms of Figure coordinates"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": null,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "x_sc = LinearScale()\n",
84 "y_sc = LinearScale()\n",
85 "\n",
86 "test_line = Lines(x=np.arange(10), y=y_data, scales={'x': x_sc, 'y': y_sc})\n",
87 "test_label = Label(x=[0.5], y=[0.2], text=['Test Label'], default_size=26,\n",
88 " font_weight='bolder', colors=['orange'])\n",
89 "\n",
90 "ax_x = Axis(scale=x_sc)\n",
91 "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
92 "\n",
93 "Figure(marks=[test_line, test_label], axes=[ax_x, ax_y])"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": null,
99 "metadata": {},
100 "outputs": [],
101 "source": [
102 "# Rotating the label\n",
103 "test_label.rotate_angle = 30"
104 ]
105 },
106 {
107 "cell_type": "markdown",
108 "metadata": {},
109 "source": [
110 "## Label positioned at a Date value"
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": null,
116 "metadata": {},
117 "outputs": [],
118 "source": [
119 "dt_sc = DateScale()\n",
120 "y_sc = LinearScale()\n",
121 "\n",
122 "lines = Lines(x=price_data.index.values, y=price_data['Security 1'].values, scales={'x': dt_sc, 'y': y_sc})\n",
123 "label = Label(x=[np.datetime64('2007-03-14')], y=[0.5], scales={'x': dt_sc}, text=['Pi-Day'], colors=['orange'])\n",
124 "\n",
125 "ax_x = Axis(scale=dt_sc)\n",
126 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
127 "\n",
128 "Figure(marks=[lines, label], axes=[ax_x, ax_y])"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": null,
134 "metadata": {},
135 "outputs": [],
136 "source": [
137 "# Setting an offset in pixel\n",
138 "label.x_offset = 100"
139 ]
140 }
141 ],
142 "metadata": {
143 "anaconda-cloud": {},
144 "kernelspec": {
145 "display_name": "Python 3",
146 "language": "python",
147 "name": "python3"
148 },
149 "language_info": {
150 "codemirror_mode": {
151 "name": "ipython",
152 "version": 3
153 },
154 "file_extension": ".py",
155 "mimetype": "text/x-python",
156 "name": "python",
157 "nbconvert_exporter": "python",
158 "pygments_lexer": "ipython3",
159 "version": "3.7.6"
160 }
161 },
162 "nbformat": 4,
163 "nbformat_minor": 1
164 }