Loading...

Pie Chart

General

Let’s begin by importing verticapy.

import verticapy as vp

Let’s also import numpy to create a random dataset.

import numpy as np

Let’s generate a dataset using the following data.

N = 100

data = vp.vDataFrame({
  "animal": [np.random.choice(['Dog','Cat','Rabbit']) for _ in range(N)],
  "color": [np.random.choice(['Black','White']) for _ in range(N)],
})

In the context of data visualization, we have the flexibility to harness multiple plotting libraries to craft a wide range of graphical representations. VerticaPy, as a versatile tool, provides support for several graphic libraries, such as Matplotlib, Highcharts, and Plotly. Each of these libraries offers unique features and capabilities, allowing us to choose the most suitable one for our specific data visualization needs.

_images/plotting_libs.png

Note

To select the desired plotting library, we simply need to use the set_option() function. VerticaPy offers the flexibility to smoothly transition between different plotting libraries. In instances where a particular graphic is not supported by the chosen library or is not supported within the VerticaPy framework, the tool will automatically generate a warning and then switch to an alternative library where the graphic can be created.

Please click on the tabs to view the various graphics generated by the different plotting libraries.

We can switch to using the plotly module.

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

In VerticaPy, you can create a diverse range of pie charts, including the option to generate nested pie charts for more complex data representations.

data["animal"].pie()

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k pie
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
data["animal"].pie(kind = "donut")

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k donut
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
data.pie(columns = ["animal", "color"])

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k nested_pie
SELECT animal, color, COUNT(*) FROM :data GROUP BY ROLLUP (1, 2);

We can switch to using the highcharts module.

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

In VerticaPy, you can create a diverse range of pie charts, including the option to generate nested pie charts for more complex data representations.

data["animal"].pie()

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k pie
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
Loading....
data["animal"].pie(kind = "donut")

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k donut
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
Loading....
data["animal"].pie(kind = "rose")

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k rose
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
Loading....
data["animal"].pie(kind = "3d")
Loading....
data.pie(columns = ["animal", "color"])

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k nested_pie
SELECT animal, color, COUNT(*) FROM :data GROUP BY ROLLUP (1, 2);
Loading....

We can switch to using the matplotlib module.

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

In VerticaPy, you can create a diverse range of pie charts, including the option to generate nested pie charts for more complex data representations.

data["animal"].pie()
Out[3]: <Axes: >
_images/plotting_matplotlib_pie_regular.png

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k pie
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
_images/plotting_matplotlib_pie_regular1.png
data["animal"].pie(kind = "donut")
Out[4]: <Axes: >
_images/plotting_matplotlib_pie_donut.png

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k donut
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
_images/plotting_matplotlib_pie_donut1.png
data["animal"].pie(kind = "rose")
Out[5]: <PolarAxes: >
_images/plotting_matplotlib_pie_rose.png

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k rose
SELECT animal, COUNT(*) FROM :data GROUP BY 1;
_images/plotting_matplotlib_pie_rose1.png
data.pie(columns = ["animal", "color"])
Out[6]: <Axes: >
_images/plotting_matplotlib_pie_nested.png

We load the VerticaPy chart extension.

%load_ext verticapy.chart

We write the SQL query using Jupyter magic cells.

%%chart -k nested_pie
SELECT animal, color, COUNT(*) FROM :data GROUP BY ROLLUP (1, 2);
_images/plotting_matplotlib_pie_nested1.png

Custom Aggregations

Within the VerticaPy framework, you have the flexibility to apply a wide array of aggregation techniques according to your specific analytical needs. This extends to the option of utilizing SQL statements, allowing you to craft custom aggregations that precisely match your data summarization requirements. VerticaPy empowers you with the versatility to aggregate data in the manner that best serves your analytical objectives.

Note

In SQL, aggregations can be computed directly within the input SQL statement, but in Python, the process is a bit different.

General Options

data["animal"].pie(method = "sum", of = "number")

Note

VerticaPy simplifies the usage of aggregations, such as percentiles. You only need to specify the percentile number without a decimal point to compute it. For instance, 50% for the median, 75% for the third quartile, and 99% for the last percentile.

Direct SQL statement

Note

You are free to utilize any SQL statement as long as it is compatible with the supported features of VerticaPy.

data["animal"].pie(method = "SUM(number) AS total")

General Options

data["animal"].pie(method = "sum", of = "number")
Loading....

Note

VerticaPy simplifies the usage of aggregations, such as percentiles. You only need to specify the percentile number without a decimal point to compute it. For instance, 50% for the median, 75% for the third quartile, and 99% for the last percentile.

Direct SQL statement

Note

You are free to utilize any SQL statement as long as it is compatible with the supported features of VerticaPy.

data["animal"].pie(method = "SUM(number) AS total")
Loading....

General Options

data["animal"].pie(method = "sum", of = "number")
Out[7]: <Axes: >
_images/plotting_matplotlib_pie_custom_agg_1.png

Note

VerticaPy simplifies the usage of aggregations, such as percentiles. You only need to specify the percentile number without a decimal point to compute it. For instance, 50% for the median, 75% for the third quartile, and 99% for the last percentile.

Direct SQL statement

Note

You are free to utilize any SQL statement as long as it is compatible with the supported features of VerticaPy.

data["animal"].pie(method = "SUM(number) AS total")
Out[8]: <Axes: >
_images/plotting_matplotlib_pie_custom_agg_2.png

Chart Customization

VerticaPy empowers users with a high degree of flexibility when it comes to tailoring the visual aspects of their plots. This customization extends to essential elements such as color schemes, text labels, and plot sizes, as well as a wide range of other attributes that can be fine-tuned to align with specific design preferences and analytical requirements. Whether you want to make your visualizations more visually appealing or need to convey specific insights with precision, VerticaPy’s customization options enable you to craft graphics that suit your exact needs.

Hint

For SQL users who use Jupyter Magic cells, chart customization must be done in Python. They can then export the graphic using the last magic cell result.

chart = _

Now, the chart variable includes the graphic. Depending on the library you are using, you will obtain a different object.

Important

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

Colors

Custom colors for 1D

fig = data["animal"].pie()
fig.update_traces(marker = dict(colors=["red"]))

Custom colors mapping for categories

Hint

You can leverage all the capabilities of the Plotly object, including functions like update_trace and ‘update_layout’.

fig = data.pie(columns = ["animal", "color"], colors=["red", "orange", "green", "blue", "yellow"])

Custom colors for 1D

data["animal"].pie(colors = ["green","blue","pink"])
Loading....

Custom colors mapping for categories

data.pie(columns = ["animal", "color"], colors = ["red", "orange", "green", "blue", "yellow"])
Loading....

Custom colors for 1D

data["animal"].pie(colors = ["red"])
Out[9]: <Axes: >
_images/plotting_matplotlib_pie_custom_color_1.png

Custom colors mapping for categories

data.pie(columns = ["animal", "color"], colors = ["red", "orange", "green", "blue", "yellow"])
Out[10]: <Axes: >
_images/plotting_matplotlib_pie_custom_color_2.png

Size

Custom Width and Height.

data.pie(columns = ["animal", "color"], width = 300, height = 300)

Custom Width and Height.

data["animal"].pie(width = 500, height = 200)
Loading....

Custom Width and Height.

data["animal"].pie(width = 6, height = 3)
Out[11]: <Axes: >
_images/plotting_matplotlib_pie_1D_custom_size.png

Text

Custom Title

data["animal"].pie().update_layout(title_text = "Custom Title")

Custom Legend Title Text

data["animal"].pie(legend_title_text = "Custom Legend")

Custom Title Text

data["animal"].pie(title = {"text": "Custom Title"})
Loading....

Custom Title Text

data["animal"].pie().set_title("Custom Title")
Out[12]: Text(0.5, 1.0, 'Custom Title')
_images/plotting_matplotlib_pie_custom_title_label.png