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?

  • Do you mind to share the data you are using? – rpanai Apr 24 '20 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. – Alex Korpas Apr 24 '20 at 14:08
  • 1
    [This](https://stackoverflow.com/a/56406926/10716823) answer may be relevant to you. – Patrick von Glehn Apr 24 '20 at 14:11
  • Does this answer your question? [Plotting times versus dates while skipping unwanted dates in Python](https://stackoverflow.com/questions/56406507/plotting-times-versus-dates-while-skipping-unwanted-dates-in-python) – rpanai Apr 24 '20 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. – Alex Korpas Apr 24 '20 at 15:01
  • If you want to break the lines of the graph, you can set `np.nan` for the dates you don't want to show. – r-beginners Apr 25 '20 at 07:24
  • @r-beginners Where do you set this? – Alex Korpas Apr 25 '20 at 10:55
  • I think you can give it a NAN for the day you want to hide it. – r-beginners Apr 25 '20 at 12:25

0 Answers0