Loading...

verticapy.jupyter.extensions.chart_magic.chart_magic#

verticapy.jupyter.extensions.chart_magic.chart_magic(line: str, cell: str | None = None, local_ns: dict | None = None) Highstock | Highchart#

Draws responsive charts using the Matplotlib, Plotly, or Highcharts library.

Different cutomization parameters are available for Plotly, Highcharts, and Matplotlib. For a comprehensive list of customization features, please consult the documentation for the respective plotting libraries: plotly, matplotlib and highcharts.

Parameters#

-c / –commandstr, optional

SQL Command to execute.

-f / –filestr, optional

Input File. You can use this option if you want to execute the input file.

-k / –kindstr, optional

Chart Type, one of the following:

  • area:

    Area Chart.

  • area_range:

    Area Range Chart.

  • area_ts:

    Area Chart with Time Series Design.

  • bar:

    Bar Chart.

  • biserial:

    Biserial Point Matrix (Correlation between binary variables and numerical)

  • boxplot:

    Box Plot.

  • bubble:

    Bubble Plot.

  • candlestick:

    Candlestick and Volumes (Time Series Special Plot).

  • cramer:

    Cramer’s V Matrix (Correlation between categories).

  • donut:

    Donut Chart.

  • donut3d:

    3D Donut Chart.

  • heatmap:

    Heatmap.

  • hist:

    Histogram.

  • kendall:

    Kendall Correlation Matrix.

    Warning

    This method uses a CROSS JOIN during computation and is therefore computationally expensive at O(n * n), where n is the total count of the vDataFrame.

  • line:

    Line Plot.

  • negative_bar:

    Multi-Bar Chart for binary classes.

  • pearson:

    Pearson Correlation Matrix.

  • pie:

    Pie Chart.

  • pie_half:

    Half Pie Chart.

  • pie3d:

    3D Pie Chart.

  • scatter:

    Scatter Plot.

  • spider:

    Spider Chart.

  • spline:

    Spline Plot.

  • stacked_bar:

    Stacker Bar Chart.

  • stacked_hist:

    Stacked Histogram.

  • spearman:

    Spearman Correlation Matrix.

-o / –outputstr, optional

Output File. You can use this option if you want to export the result of the query to the HTML format.

Returns#

Chart Object

Examples#

The following examples demonstrate:

  • Setting up the environment

  • Drawing graphics

  • Exporting to HTML

  • Using variables

  • Using SQL files

Hint

To see more examples, please refer to the ref:chart_gallery.guide.

Setting up the environment#

If you don’t already have one, create a new connection:

import verticapy as vp

# Save a new connection
vp.new_connection(
    {
        "host": "10.211.55.14",
        "port": "5433",
        "database": "testdb",
        "password": "XxX",
        "user": "dbadmin",
    },
    name = "VerticaDSN",
)

Otherwise, to use an existing connection:

vp.connect("VerticaDSN")

Load the chart extension:

Run the following to load some sample datasets. Once loaded, these datasets are stored in the ‘public’ schema. You can change the target schema with the ‘schema’ parameter:

from verticapy.datasets import load_titanic, load_amazon, load_iris

titanic = load_titanic()

amazon = load_amazon()

iris = load_iris()

Use the set_option() function to set your desired plotting library:

vp.set_option("plotting_lib","plotly")

Drawing graphics#

The following examples draw various responsive charts from SQL queries.

Pie Chart#

%chart -k pie -c "SELECT pclass, AVG(age) AS av_avg FROM titanic GROUP BY 1;"

Line Plot#

%%chart -k line
SELECT
    date,
    AVG(number) AS number
FROM amazon
GROUP BY 1;

Correlation Matrix#

%%chart -k pearson
SELECT
    *
FROM titanic;

Bar Chart#

%%chart -k bar
SELECT
    pclass,
    SUM(survived)
FROM titanic GROUP BY 1;

Scatter Plot#

%%chart -k scatter
SELECT
    PetalLengthCm,
    PetalWidthCm,
    Species
FROM iris;

Boxplot#

%%chart -k boxplot
SELECT * FROM titanic;

Exporting to HTML#

Export a chart to HTML:

%%chart -k scatter -o "my_graphic"
SELECT * FROM titanic;

The following lines open the HTML file:

Note

The HTML graphic can be embedded in an external environment, such as a website.

file = open("my_graphic.html", "r")
file.read()
file.close()

Using Variables#

You can use variables in charts with the ‘:’ operator:

import verticapy.sql.functions as vpf

class_fare = titanic.groupby(
    "pclass",
    [vpf.avg(titanic["fare"])._as("avg_fare")],
)

123
pclass
Integer
123
avg_fare
Float(22)
1188.3537128205128
2221.4416984555985
3313.2290019637462
Rows: 1-3 | Columns: 2

You can then use the variable in the query:

Note

In this example, we use a vDataFrame, but it’s also possible to use a pandas.DataFrame, a numpy.array, and many other in-memory objects.

%%chart -k bar
SELECT * FROM :class_fare;

Using SQL files#

Create charts from a SQL file:

file = open("/project/data/VerticaPy/docs/query.sql", "w+")

file.write("SELECT PetalLengthCm, PetalWidthCm, Species FROM iris;")
Out[9]: 54

file.close()
file = open("query.sql", "w+")
file.write("SELECT PetalLengthCm, PetalWidthCm, Species FROM iris;")
file.close()

Using the -f option, we can easily read the above SQL file:

%chart -f query.sql -k scatter