from canvasxpress.canvas import CanvasXpress
from canvasxpress.render.jupyter import CXNoteBook
import pandas as pd
import requests
from uuid import uuid4
import json
CanvasXpress Matrix Data Options
Import Core Packages for this Demo
Common Chart Components
Establish essential configurations to avoid chart creation repetition:
= {
mpg_cx_config "aes": {
"x": "displ",
"y": "hwy",
"color": "class"
},"colorBy": "class",
"graphOrientation": "vertical",
"graphType": "Scatter2D",
"isCxplot": True,
"remoteTransitionEffect": "none",
"smpLabelRotate": 90,
"sortOnGrouping": "ascending",
"theme": "GGPlot",
"transformAxis": False,
"widthFactor": 2,
"xAxis": ["displ"],
"xAxisTitle": "displ",
"yAxis": ["hwy"],
"yAxisTitle": "hwy",
}
= [
mpg_cx_after_render
["geom_point",
[
{"color": "class"
},None,
None
],
],
["gg_labels",
[
{"xAxisTitle": "displ",
"yAxisTitle": "hwy"
}
],
],
["drawRangesAfterWheelEvent",
None,None,None,None,0],
[
],
["clickGraphMaxMin",
False,False,True],
[
] ]
XYZ data dict
Arrange data into a Python dict formatted to CanvasXpress XYZ.
Show the code
= pd.read_csv("https://raw.githubusercontent.com/sidsriv/Introduction-to-Data-Science-in-python/master/mpg.csv", index_col=0)
mpg_df
= {
mpg_xyz_data "y": {
"vars": ["displ","hwy"],
"smps": [i+1 for i in range(mpg_df.index.size)],
"data": [
"displ"].to_list(),
mpg_df["hwy"].to_list(),
mpg_df[
]
},"x": {
factor: mpg_df[factor].to_list()for factor in mpg_df.columns
if factor not in ["displ", "hwy"]
},
}
= CanvasXpress(
example_df_cx =str(uuid4()),
render_to=mpg_xyz_data,
data= mpg_cx_config,
config = mpg_cx_after_render,
after_render =500,
width=400,
height
)
= CXNoteBook(example_df_cx)
example_df_cx_renderer example_df_cx_renderer.render()
URL pass-through to CanvasXpress
Obtain the CSV data as a URL (text) and pass it through to CanvasXpress:
Show the code
from canvasxpress.data.url import CXUrlData
= CanvasXpress(
example_mpg_csv_df =str(uuid4()),
render_to=CXUrlData("https://raw.githubusercontent.com/sidsriv/Introduction-to-Data-Science-in-python/master/mpg.csv"),
data= mpg_cx_config,
config = mpg_cx_after_render,
after_render =500,
width=400,
height
)
= CXNoteBook(example_mpg_csv_df)
example_mpg_csv_df_renderer example_mpg_csv_df_renderer.render()
Raw text pass-through to CanvasXpress
Obtain the CSV data as raw text and pass it through to CanvasXpress for interpretation:
Show the code
from canvasxpress.data.text import CXTextData
= requests.get("https://raw.githubusercontent.com/sidsriv/Introduction-to-Data-Science-in-python/master/mpg.csv").content.decode("UTF-8")
mpg_text
= CanvasXpress(
example_text_cx =str(uuid4()),
render_to=CXTextData(mpg_text),
data= mpg_cx_config,
config = mpg_cx_after_render,
after_render =500,
width=400,
height
)
= CXNoteBook(example_text_cx)
example_text_cx_renderer example_text_cx_renderer.render()
JSON pass-through to CanvasXpress
Obtain the CSV data as a CSV (text) and transform it for CanvasXpress:
Show the code
from canvasxpress.data.keypair import CXJSONData
= pd.read_csv("https://raw.githubusercontent.com/sidsriv/Introduction-to-Data-Science-in-python/master/mpg.csv", index_col=0)
mpg_df
= {
mpg_xyz_data "y": {
"vars": ["displ","hwy"],
"smps": [i+1 for i in range(mpg_df.index.size)],
"data": [
"displ"].to_list(),
mpg_df["hwy"].to_list(),
mpg_df[
]
},"x": {
factor: mpg_df[factor].to_list()for factor in mpg_df.columns
if factor not in ["displ", "hwy"]
},
}
= CanvasXpress(
example_mpg_json =str(uuid4()),
render_to=CXJSONData(json.dumps(mpg_xyz_data)),
data= mpg_cx_config,
config = mpg_cx_after_render,
after_render =500,
width=400,
height
)
= CXNoteBook(example_mpg_json)
example_mpg_json_renderer example_mpg_json_renderer.render()
Pandas DataFrame with metadata
Use a Pandas DataFrame and pass with it meta X data.
Show the code
from canvasxpress.data.matrix import CXDataframeData
= pd.read_csv("https://raw.githubusercontent.com/sidsriv/Introduction-to-Data-Science-in-python/master/mpg.csv", index_col=0)
df_all
= CXDataframeData(
mpg_dfd "displ", "hwy"]]).transpose()
(df_all[[
)
vars=["displ", "hwy"]
mpg_dfd.profile.= {
mpg_dfd.profile.x
factor: df_all[factor].to_list()for factor in df_all.columns
if factor not in ["displ", "hwy"]
}
= CanvasXpress(
example_dfd_cx =str(uuid4()),
render_to=mpg_dfd,
data=mpg_cx_config,
config=mpg_cx_after_render,
after_render=500,
width=400,
height
)
= CXNoteBook(example_dfd_cx)
example_dfd_cx example_dfd_cx.render()