0

I am having problems to adjust the datetime in a better way to visualize in my graph. Here is my code:

fig = plt.figure()
new.plot(title='(Graph)',figsize=(10,7), legend=None)

plt.tick_params(axis="both", which="both", bottom="off", top="off",    
                            labelbottom="on", left="off", right="off", labelleft="on")   
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5), frameon=False)
ax = plt.subplot(111)   
ax.spines["top"].set_visible(False)    
ax.spines["bottom"].set_visible(True)    
ax.spines["right"].set_visible(False)    
ax.spines["left"].set_visible(False)    
ax.get_xaxis().tick_bottom()    
ax.get_yaxis().tick_left()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
pp.savefig(bbox_inches='tight')
pp.close()

I tried to add this library and in the ax nominations:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)

enter image description here

2 Answers 2

1

You can try rotating the labels adding the paramenter labelrotation https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.tick_params.html:

plt.tick_params(axis="both", which="both", bottom="off", top="off",    
                        labelbottom="on", left="off", right="off", 
                        labelleft="on", labelrotation=45)
Sign up to request clarification or add additional context in comments.

5 Comments

This does not work raises an error. TypeError: __init__() got an unexpected keyword argument 'rotation'
try with labelrotation. Maybe it depends from the version
keyword labelrotation is not recognized; valid keywords are ['size', 'width', 'color', 'tickdir', 'pad', 'labelsize', 'labelcolor', 'zorder', 'gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On', 'length', 'direction', 'left', 'bottom', 'right', 'top', 'labelleft', 'labelbottom', 'labelright', 'labeltop']
Thats weid, also because you use the keyword which, which is not listed. Can you try with tickdir ?
0

I just added the following line and it did it automatically

plt.gcf().autofmt_xdate()   

Comments

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.