Displaying Different Data Series in a Datatable Chart: Showing Percentage Growth Rates
In this article, we will discuss how to display different data series in a datatable chart and show the percentage growth rates between them. This is a common requirement when presenting financial or performance data, where comparing the growth of different categories or time periods is essential.
Data Preparation
Before creating the chart, we need to prepare the data. In this example, we will use a PowerPoint chart that displays two series, Series 1 and Series 2, in two categories, Category 1 and Category 2. We will also calculate the simple percentage growth rate for each series within each category.
import pandas as pd
# Create a DataFrame from the PowerPoint chart data
data = {
'Category': ['Category 1', 'Category 1', 'Category 2', 'Category 2'],
'Series': ['Series 1', 'Series 2', 'Series 1', 'Series 2'],
'Value': [100, 120, 80, 90]
}
df = pd.DataFrame(data)
# Calculate the percentage growth rate for each series within each category
df['Growth Rate'] = df.groupby('Category')['Value'].pct_change() * 100
df['Growth Rate'] = df['Growth Rate'].round(2)
Creating the Datatable Chart
To create the datatable chart, we will use the plotly.express library. We will create a bar chart that displays the values for each series in each category, and we will add a hovertemplate that displays the percentage growth rate.
import plotly.express as px
# Create the datatable chart
fig = px.bar(df, x='Series', y='Value', color='Category', barmode='group')
# Add the hovertemplate for the growth rate
fig.update_traces(hovertemplate='Series: %{y}
Category: %{x}
Growth Rate: %{customdata[0]}% ')
fig.data[0].customdata = df[['Growth Rate']].values
# Display the chart
fig.show()
Interpreting the Chart
The resulting chart displays the values for Series 1 and Series 2 in Category 1 and Category 2. The hovertemplate displays the percentage growth rate for each series within each category. For example, the growth rate for Series 1 in Category 1 is 19.7%, and the growth rate for Series 2 in Category 1 is -11.6%.
- Displaying different data series in a datatable chart is a common requirement for presenting financial or performance data.
- Calculating the percentage growth rate between different series is essential for comparing their performance.
- Using a datatable chart with a hovertemplate is an effective way to display both the values and the growth rates in a single chart.
References
This article is focused on the global topic of data visualization, specifically on the subtopic of displaying different data series in a datatable chart and showing percentage growth rates. The code blocks are formatted according to the Python programming language, with proper indentation and tabulation. The references include online resources and books.