Density¶
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({
"category": [np.random.choice(['A','B','C']) for _ in range(N)],
"score1": np.random.normal(5, 1, N),
"score2": np.random.normal(8, 1.5, N),
"score3": np.random.normal(10, 2, 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.
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 have the flexibility to generate either a single density plot or multiple density plots within a single graphical representation.
data["score1"].density()
data.density(columns = ["score1", "score2", "score3"])
We can switch to using the highcharts module.
vp.set_option("plotting_lib", "highcharts")
In VerticaPy, you have the flexibility to generate either a single density plot or multiple density plots within a single graphical representation.
data["score1"].density()
data.density(columns = ["score1", "score2", "score3"])
We can switch to using the matplotlib module.
vp.set_option("plotting_lib", "matplotlib")
In VerticaPy, you have the flexibility to generate either a single density plot or multiple density plots within a single graphical representation.
data["score1"].density()
Out[3]: <Axes: xlabel='score1', ylabel='density'>
data.density(columns = ["score1", "score2", "score3"])
Out[4]: <Axes: title={'center': 'KernelDensity'}, ylabel='density'>
Custom Parameters¶
In VerticaPy, there are various customization options available for tailoring your density plots to your specific needs. You can customize the bandwidth to control the smoothness of the plot, allowing you to emphasize or reduce the level of detail. Additionally, you have the choice to select the kernel from a range of options, providing flexibility in how the density is estimated and displayed.
Bandwidth
data["score1"].density(bandwidth = 0.4)
Note
You can adjust the bandwidth in VerticaPy to fine-tune the level of smoothness or granularity in your density plots, allowing you to precisely control the appearance of your visualizations.
Kernel
Note
In VerticaPy, you have the flexibility to choose from various kernel options when creating density plots. This choice of kernel allows you to tailor the density estimation method to best represent your data distribution, ensuring your visualizations accurately capture the underlying patterns in your data.
data["score1"].density(kernel='logistic')
Bandwidth
data["score1"].density(bandwidth=0.4)
Note
You can adjust the bandwidth in VerticaPy to fine-tune the level of smoothness or granularity in your density plots, allowing you to precisely control the appearance of your visualizations.
Kernel
Note
In VerticaPy, you have the flexibility to choose from various kernel options when creating density plots. This choice of kernel allows you to tailor the density estimation method to best represent your data distribution, ensuring your visualizations accurately capture the underlying patterns in your data.
data["score1"].density(kernel='logistic')
Bandwidth
data["score1"].density(bandwidth=0.4)
Out[5]: <Axes: xlabel='score1', ylabel='density'>
Note
You can adjust the bandwidth in VerticaPy to fine-tune the level of smoothness or granularity in your density plots, allowing you to precisely control the appearance of your visualizations.
Kernel
Note
In VerticaPy, you have the flexibility to choose from various kernel options when creating density plots. This choice of kernel allows you to tailor the density estimation method to best represent your data distribution, ensuring your visualizations accurately capture the underlying patterns in your data.
data["score1"].density(kernel='logistic')
Out[6]: <Axes: xlabel='score1', ylabel='density'>
Grouping¶
Group by categories.
data["score1"].density(by = "category")
Group by categories.
data["score1"].density(by = "category")
Group by categories.
data["score1"].density(by = "category")
Out[7]: <Axes: title={'center': 'KernelDensity'}, xlabel='score1', ylabel='density'>
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.
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["score1"].density()
fig.update_traces(marker = dict(color="red"))
Custom colors mapping for categories
Note
You can leverage all the capabilities of the Plotly object, including functions like update_trace.
fig = data.density(columns = ["score1", "score2", "score3"])
new_colors = ["red", "orange","green"]
for trace_index, new_color in enumerate(new_colors):
if trace_index < len(fig.data):
fig.data[trace_index].marker.color = new_color
Custom colors for 1D
data["score1"].density(colors = ["green"])
Custom colors mapping for categories
data.density(columns = ["score1", "score2", "score3"], colors = ["red", "orange", "green"])
Custom colors for 1D
data["score1"].density(color = ["red"])
Out[8]: <Axes: xlabel='score1', ylabel='density'>
Custom colors mapping for categories
data.density(columns = ["score1", "score2", "score3"], color = ["red", "orange", "green"])
Out[9]: <Axes: title={'center': 'KernelDensity'}, ylabel='density'>
Size¶
Custom Width and Height.
data.density(columns = ["score1", "score2", "score3"], width = 300, height = 300)
Custom Width and Height.
data["grade"].density(width = 500, height = 200)
Custom Width and Height.
data["score1"].density(width = 6, height = 3)
Out[10]: <Axes: xlabel='score1', ylabel='density'>
Text¶
Custom Title
data["score1"].density(title_text = "Custom Title")
Custom Legend Title Text
data.density(columns = ["score1", "score2", "score3"], legend_title_text = "Custom Legend")
Custom Axis Titles
data.density(columns = ["score1", "score2", "score3"], yaxis_title = "Custom Y-Axis Title")
Custom Title Text
data["score1"].density(title = {"text": "Custom Title"})
Custom Axis Titles
data["score1"].density(xAxis = {"title": {"text": "Custom X-Axis Title"}})
Custom Title Text
data["score1"].density().set_title("Custom Title")
Out[11]: Text(0.5, 1.0, 'Custom Title')
Custom Axis Titles
data["score1"].density().set_ylabel("Custom Y Axis")
Out[12]: Text(0, 0.5, 'Custom Y Axis')