Python charting libraries¶
This workbook details example of interfacing PyKX with Python charting libraries.
PyKX supports rich datatype mapping meaning you can convert data from PyKX objects to:
- Python objects using
.py()
- NumPy objects using
.np()
- Pandas objects using
.pd()
- PyArrow objects using
.pa()
The full breakdown of how these map is documented here.
The resulting objects behave as expected with all Python libraries.
For efficiency and exactness the examples below aim to use PyKX objects directly, minimising conversions when possible.
import pykx as kx
tab = kx.Table(data={
'sym':kx.random.random(1000, ['a', 'b', 'c']),
'price':kx.random.random(1000, 1.0),
'size':kx.random.random(1000, 10),
'quantity':kx.random.random(1000,100),
'in_stock':kx.random.random(1000, [True, False])})
tab.head()
sym | price | size | quantity | in_stock | |
---|---|---|---|---|---|
0 | a | 0.9094126 | 4 | 5 | 1b |
1 | a | 0.2988477 | 5 | 18 | 1b |
2 | c | 0.454063 | 8 | 11 | 0b |
3 | b | 0.156942 | 1 | 36 | 1b |
4 | c | 0.04699265 | 4 | 43 | 1b |
Matplotlib¶
Let's generate a scatter plot using the price
and size
columns of our table.
Use the scatter(tab['price'], tab['quantity'])
notation to access PyKX objects directly.
The x=
and y=
syntax requires conversion to a dataframe using .pd()
.i.e scatter(tab.pd(), x='price' ,y='quantity')
scatter
uses a series of 1D arrays and one of the only charts where you don't need to start by converting the column values in NumPy objects using .np()
.
import matplotlib.pyplot as plt
plt.scatter(tab['price'], tab['quantity'])
plt.show()
To make the column values compatible with most Matplotlib charts, first convert them to NumPy objects using the .np()
function.
plt.bar(tab['size'].np(), tab['price'].np())
plt.show()
import plotly.express as px
fig = px.scatter(
x=tab['quantity'],
y=tab['price'],
size=tab['size'],
color=tab['sym'])
fig.show(renderer="png")