Coder Social home page Coder Social logo

not working with plot_date about adjusttext HOT 11 CLOSED

phlya avatar phlya commented on September 28, 2024
not working with plot_date

from adjusttext.

Comments (11)

manu2000 avatar manu2000 commented on September 28, 2024

BTW I am using Python 3.5.

from adjusttext.

manu2000 avatar manu2000 commented on September 28, 2024

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'.

from adjusttext.

Phlya avatar Phlya commented on September 28, 2024

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.

from adjusttext.

manu2000 avatar manu2000 commented on September 28, 2024

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()

from adjusttext.

Phlya avatar Phlya commented on September 28, 2024

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?

from adjusttext.

manu2000 avatar manu2000 commented on September 28, 2024

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.

from adjusttext.

Phlya avatar Phlya commented on September 28, 2024

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

from adjusttext.

manu2000 avatar manu2000 commented on September 28, 2024

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()

from adjusttext.

Phlya avatar Phlya commented on September 28, 2024

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()

from adjusttext.

manu2000 avatar manu2000 commented on September 28, 2024

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

from adjusttext.

Phlya avatar Phlya commented on September 28, 2024

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

from adjusttext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.