0

I am trying to plot average temperatures of 3 months (January, February and December 2017) with x being each day of the month (type of data is datetime64[ns]). the number of x is 90 so I have 90 dates. Below is my code:

import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib import dates as mpl_dates
date_format=mpl_dates.DateFormatter('%B')

#convert data to datetime.objects
x_dates.loc[:,["YEAR","DAY","MONTH"]]=x_dates.loc[:,["YEAR","DAY","MONTH"]]
x_dates1=pd.to_datetime(x_dates)
x_dates1=pd.DataFrame({"DATE":x_dates1}

#take the mean temperatures
ymean_winter=np.array(Winter["TEMP"])

#create and format figure
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot_date(x_dates1['DATE'],ymean_winter,linestyle='solid')
fig.autofmt_xdate()
plt.gca().xaxis.set_major_formatter(date_format)
plt.show()

Once I code the above I get the below:

Average temperature in Winter 2017

The problem is that I get this interval which I do not wish to have (March 2017 to November 2017). If I convert the dates to strings I don't have the issue but then I face the issue of either removing the x-ticks as I have too many x-ticks that they overlap each other and I can't discern the dates or enlarge the figure like fig=plt.figure(figsize=(35,35)) to be able to see each x point.

How can I remove the empty space?

8
  • Do you mind to share the data you are using? Commented Apr 24, 2020 at 13:55
  • Not sure how to attach the file of the data. It's basically average temperatures of 2017 of months December, February and January. Commented Apr 24, 2020 at 14:08
  • 1
    This answer may be relevant to you. Commented Apr 24, 2020 at 14:11
  • Does this answer your question? Plotting times versus dates while skipping unwanted dates in Python Commented Apr 24, 2020 at 14:37
  • Not really. I see in the graph of the example that the empty dates still appear (3rd of February until 7). Looks like what I am trying to avoid. I want the next data point from February 28th 2017 to be December 1st 2017. Commented Apr 24, 2020 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.