Math Lover
Math Lover

Reputation: 177

Seaborn plot adds extra zeroes to x axis time-stamp labels

I am trying to plot the below dataset as barplot cum pointplot using seaborn.

enter image description here

But the time-stamp in the x-axis labels shows additional zeroes at the end as shown below

enter image description here

The code I use is

import matplotlib.pyplot as plt
import seaborn as sns
fig, ax1 = plt.subplots()
# Plot the barplot
sns.barplot(x='Date', y=y_value, hue='Sentiment', data=mergedData1, ax=ax1)
# Assign y axis label for bar plot
ax1.set_ylabel('No of Feeds')
# Position the legen on the right side outside the box
plt.legend(loc=2, bbox_to_anchor=(1.1, 1), ncol=1)
# Create a dual axis
ax2 = ax1.twinx()
# Plot the ponitplot
sns.pointplot(x='Date', y='meanTRP', data=mergedData1, ax=ax2, color='r')
# Assign y axis label for point plot
ax2.set_ylabel('TRP')
# Hide the grid for secondary axis
ax2.grid(False)
# Give a chart title
plt.title(source+' Social Media Feeds & TRP for the show '+show)
# Automatically align the x axis labels
fig.autofmt_xdate()
fig.tight_layout()

Not sure what is going wrong. Please help me with this. Thanks

Upvotes: 6

Views: 1869

Answers (2)

Jose Rondon
Jose Rondon

Reputation: 390

You can still have more control over date format with this code:

ax.set_xticklabels([pd.to_datetime(tm).strftime('%d-%m-%Y') for tm in ax.get_xticklabels()])

Upvotes: 0

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339450

Easiest solution is to split the text at the letter "T" as the rest is probably not needed.

ax.set_xticklabels([t.get_text().split("T")[0] for t in ax.get_xticklabels()])

Upvotes: 7

Related Questions