Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working with plot_date #14

Closed
manu2000 opened this issue Jan 6, 2017 · 11 comments
Closed

not working with plot_date #14

manu2000 opened this issue Jan 6, 2017 · 11 comments

Comments

@manu2000
Copy link

manu2000 commented Jan 6, 2017

Thanks for creating such an awesome tool!
I am trying to use it with plot_date, however I get this error:

File "C:\Anaconda3\lib\site-packages\adjustText\adjustText.py", line 407, in adjust_text
ax=ax)

File "C:\Anaconda3\lib\site-packages\adjustText\adjustText.py", line 111, in optimally_align_text
c = len(get_points_inside_bbox(x, y, bbox))

File "C:\Anaconda3\lib\site-packages\adjustText\adjustText.py", line 24, in get_points_inside_bbox
x_in = np.logical_and(x>x1, x<x2)

File "pandas\tslib.pyx", line 941, in pandas.tslib._Timestamp.richcmp (pandas\tslib.c:18619)

TypeError: Cannot compare type 'Timestamp' with type 'float'

I also tried to set locale as you recommended in the example using:
import locale locale.setlocale(locale.LC_ALL,'en_GB.utf8')
but I get this error:

File "", line 2, in
locale.setlocale(locale.LC_ALL,'en_GB.utf8')

File "C:\Anaconda3\lib\locale.py", line 594, in setlocale
return _setlocale(category, locale)

Error: unsupported locale setting

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

BTW I am using Python 3.5.

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

Looks like the code that you used for changing locale is for linux. These are the codes for windows .

Using these codes I was able to change the locale settings but I am still getting the same error as above when using adjustText. I tried both 'english-uk' and 'english-us'.

@Phlya
Copy link
Owner

Phlya commented Jan 6, 2017

Can you provide the code to reproduce this error?
I never use anything with time series myself, so don't know much about this...
And about the locale, I had the problem because mine was set to Russian, if your system in in English it should be fine I think.

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

Here is some sample code. I am using Windows and python 3.5.

import matplotlib.pyplot as plt
from adjustText import adjust_text
import datetime
import random

x = [datetime.datetime.now() + datetime.timedelta(days=i) for i in range(30)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]

plt.plot_date(x,y)

plt.gcf().autofmt_xdate()

texts = []
for i,_ in enumerate(x):
    texts.append(plt.text(x[i],y[i],round(y[i],3),fontsize=8))   
adjust_text(texts, arrowprops=dict(arrowstyle="-", color='r', lw=0.5))

plt.show()

@Phlya
Copy link
Owner

Phlya commented Jan 6, 2017

Can you try running the code from the examples? As I said, I have exactly zero experience with date/time plotting, so I don't know - could there be a difference between dates with mpl.mdates and datetime?

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

You are right. The error was because I need to convert the dates to number using
mdates.date2num() before passing it to adjust_text(). After doing this I am not getting any error.

@manu2000 manu2000 closed this as completed Jan 6, 2017
@Phlya
Copy link
Owner

Phlya commented Jan 6, 2017

Glad to hear, thanks! Can you show the final code that works here for future reference?

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

Sure. Here is a working example with plot_date:

import matplotlib.pyplot as plt
from adjustText import adjust_text
import datetime
import random
import matplotlib.dates as mdates

# make up some data
x = [datetime.datetime.now() + datetime.timedelta(days=i) for i in range(30)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]

# plot
plt.plot_date(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

texts = []
for i,_ in enumerate(x):
    texts.append(plt.text(mdates.date2num(x[i]),y[i],round(y[i],3),fontsize=8))   
adjust_text(texts, arrowprops=dict(arrowstyle="-", color='r', lw=0.5))

plt.show()

@Phlya
Copy link
Owner

Phlya commented Jan 6, 2017

Thank you!
This is better then, I guess? Seems like it works the same.

import matplotlib.pyplot as plt
from adjustText import adjust_text
import datetime
import random
import matplotlib.dates as mdates

# make up some data
x = [datetime.datetime.now() + datetime.timedelta(days=i) for i in range(30)]
y = [i+random.gauss(0,1) for i,_ in enumerate(x)]

x = mdates.date2num(x)

# plot
plt.plot_date(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

texts = []
for i,_ in enumerate(x):
    texts.append(plt.text(x[i], y[i], round(y[i],3), fontsize=8))   
adjust_text(texts, arrowprops=dict(arrowstyle="-", color='r', lw=0.5))

plt.show()

@manu2000
Copy link
Author

manu2000 commented Jan 6, 2017

mdates.date2num(x) is not needed, that was a typo. Infact x = mdates.date2num(x) will give an error. I will edit my comment.

@Phlya
Copy link
Owner

Phlya commented Jan 6, 2017

For me it works and doesn't require converting each x value separately when adding texts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants