Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

seaborn

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

like image 464
Sreenath1986 Avatar asked Sep 26 '17 12:09

Sreenath1986


People also ask

How to set axis labels in Seaborn plot using Python?

Method 1: To set the axes label in the seaborn plot, we use matplotlib.axes.Axes.set () function from the matplotlib library of python. xlabel : str- The label text for the x-axis. ylabel : str- The label text for the y-axis. Returns: It will change the x-axis and y-axis labels.

What are axes limits in Seaborn plot?

Axes Limits are the limits to the axes’ values, which are used to filter for a required value on axes. Here, In this article, the content goes from setting the axes labels, axes limits, and both at a time. In the end, you will be able to learn how to set axes labels & limits in a Seaborn plot.

How to change the axis labels and title of a plot?

Make sure to assign the axes-level object while creating the plot. This object is then used for setting the title and labels as shown below. We can also change the axis labels and set the plot title with the matplotlib.pyplot object using xlabel (), ylabel () and title () functions.

How do you plot a line graph in Seaborn?

To create a line plot with Seaborn we can use the lineplot method, as previously mentioned. Here’s a working example plotting the x variable on the y-axis and the Day variable on the x-axis: Here we started with the simplest possible line graph using Seaborn’s lineplot.


1 Answers

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()])
like image 84
ImportanceOfBeingErnest Avatar answered Oct 17 '22 02:10

ImportanceOfBeingErnest