Reputation: 1
I have a presentation that I have to update every month. There are some slides in this presentation with a line plot like the one below:
In this slide, I must update the chart data every month and then write the values of the last point in the rightmost side of the plot as shown in the image. So for example the values for the next month should be different from the values of the current month. The chart I use is a Powerpoint chart
, so I can apply some style on it and only changes the data using python.
What I already can do:
• Update the data using CategoryChartData
without changing the style of the plot;
• Keep or change the colors and width of the lines;
• Write the datalabels
in the right like I already said.
The problem in the last point is: to add the last points’ datalabels and hide all the other ones, I need to start with a plot without any datalabel. In other words, I can only add a datalabel but I can’t remove any. The problem with this is that when I update the next month the datalabel value from this month will remain in the plot. Here is another example:
Here, the datalabels in the middle are the last datapoints from the last day of January. When I updated the plot with the values from May, I should have deleted those datalabels and keep only the ones from the last data point from May.
Yes, I could use a template without any datalabels and only add the ones I want to generate a final copy, but the problem is in this project this is quite unpractical since it is a big project and the presentation must be updated over the last one. I’ve already tried many things, like:
points[i].data_label.has_text_frame = False
points[i].data_label.show_value = False
chart_.plots[0].data_labels.show_percentage = False
series_.data_labels.show_value = False
But none worked. I've also tried this ideia: python-pptx – How to individually hide/show data labels in graph but it didn't work since redefining the text kind of makes me lose the original style of the layout and is harder for me to style by code.
Just to be clear, I already did this in matplotlib but it was very hard to maintain the code since here we reuse the same plot with different styles, so the solution to use python-pptx
is the best one.
What I would like is to hide all datalabels but the last ones. This is valid for datalabels that already are on chart, like on the second print above.
Is there any way to solve this? Like hide all datalabels and then activate only the last one?
Upvotes: 0
Views: 132
Reputation: 1789
I appreciate you said you tried with matplotlib, but have you tried using plt.text
? Something like:
import matplotlib.pyplot as plt
import pandas as pd
# Example time-series data
data = {'Date': pd.date_range(start='1/1/2020', periods=10, freq='M'),
'Value': [10, 15, 13, 17, 19, 21, 25, 22, 26, 28]}
df = pd.DataFrame(data)
# Plot the time-series data
plt.figure(figsize=(10, 6))
plt.plot(df['Date'], df['Value'], marker='o')
# Annotate the last data point
last_date = df['Date'].iloc[-1]
last_value = df['Value'].iloc[-1]
plt.text(last_date, last_value, f'{last_value}', fontsize=12, ha='left')
# Display the plot
plt.show()
Otherwise, it might be helpful to include your python-pptx
code
Upvotes: 0