0

The year on the x-axis is incorrect; the table format is YY-MM-DD. Which part of the code do I miss? Could you please make a suggestion?

date    new_sentiment   count
0   2020-03-06  POSITIVE    1
1   2020-03-17  NEGATIVE    2
2   2020-03-18  NEUTRAL 1
3   2020-03-21  NEUTRAL 1
4   2020-03-23  NEUTRAL 1
... ... ... ...
831 2021-05-30  NEGATIVE    92
832 2021-05-30  NEUTRAL 84
833 2021-05-30  POSITIVE    26
834 2021-05-31  NEGATIVE    19
835 2021-05-31  NEUTRAL 7

my code

sns.lineplot(data=data, x='date', y='count', hue='new_sentiment')
sns.set_style("whitegrid", {
    "ytick.major.size": 0.1,
    "ytick.minor.size": 0.05,
    'grid.linestyle': 'solid'
 })
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")
plt.setp(plt.gca().xaxis.get_majorticklabels(),rotation=90)
myFmt = mdates.DateFormatter('%d-%b-%Y')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=16))
plt.xlim('2020-03-06','2021-06-15')
plt.savefig('04_#clean_sentiment_count_2020_2021.tiff', dpi=300, format='tiff', bbox_inches='tight')

Output Graph

tassaneel
  • 175
  • 2
  • 14
  • change this myFmt = mdates.DateFormatter('%d-%b-%Y') to myFmt = mdates.DateFormatter('%Y-%m-%d') – virxen Jun 18 '21 at 12:11
  • it did not work. – tassaneel Jun 18 '21 at 18:26
  • 1
    check this https://www.geeksforgeeks.org/matplotlib-dates-dateformatter-class-in-python/ – virxen Jun 18 '21 at 18:50
  • Thanks for the suggestion. I foud the answer -- I add `data['date'] = pd.to_datetime(data['date']).dt.date` in the first line and convert `plt.xlim('2020-03-06','2021-06-15')` to `plt.xlim([datetime.date(2020, 3, 13), datetime.date(2021, 6, 15)])` – tassaneel Jun 18 '21 at 18:54

0 Answers0