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

2 Feature Requests: Plot Extra Grid Lines and deal with 'None' Values in Data Plot #65

Closed
NLKNguyen opened this issue Dec 24, 2021 · 7 comments

Comments

@NLKNguyen
Copy link

Hi,

The DateTime plot plot_date requires all values to be numeric (None/NaN/np.nan is not accepted), but it's useful to be able to display shorter lines that don't span the whole time frame, which requires a way to represent no data like None/NaN. What I'm trying to do is to add some arbitrary trend line/channel on a stock price series.
Can you support this?

Thanks!

@piccolomo
Copy link
Owner

Hi @NLKNguyen

thanks a lot for the issue report. However I find it a bit difficult to understand it. Could you kindly restate in simpler or different terms?

in particular, by values, you mean y values (not datetime ones)? if some y values are none/nan, how will this make the plot lines shorter? it seems to me, it would rather remove some of them (the one containing none/nan values) rather then shortening.

Thanks for help

@NLKNguyen
Copy link
Author

Thanks for the quick response,

I realized I used your API incorrectly. There is no issue. I'm used to other chart libraries that require all time series to share the same time frame and where there is no data, a NaN or None is expected.

Turned out multiple plot_date can have different dates as long as each has the same data length and date length. Here is just for reference.

import pandas as pd
import plotext as plt

plt.datetime.set_datetime_form(date_form='%Y-%m-%d')

t1 = pd.date_range("2022-01-01", periods=5, freq="D")
x1 = [plt.datetime.datetime_to_string(el) for el in t1]
y1 = pd.Series([1,2,3,4,5], index=t1).tolist()
plt.plot_date(x1, y1,  marker = "hd", color=[1])

t2 = pd.date_range("2022-01-06", periods=5, freq="D") 
x2 = [plt.datetime.datetime_to_string(el) for el in t2]
y2 = pd.Series([6,7,8,9,10], index=t2).tolist()
plt.plot_date(x2, y2,  marker = "hd", color=[4])

t3 = pd.date_range("2022-01-03", periods=4, freq="D") 
x3 = [plt.datetime.datetime_to_string(el) for el in t3]
y3 = pd.Series([4,5,6,7], index=t3).tolist()
plt.plot_date(x3, y3,  marker = "hd", color=[3])

plt.canvas_color(0) 
plt.axes_color(0)
plt.ticks_color("bright-yellow")
plt.show()

image

Another question though, is there a way to show vertical lines on certain dates or horizontal lines on certain values? That would be similar to the existing Grid feature but only on specific locations.

Here is an example of an arbitrary vertical line when I use another lib called uniplot
image

@piccolomo
Copy link
Owner

piccolomo commented Dec 27, 2021

Hi @NLKNguyen, I have read your message and I liked your inputs, including the first one, which I think it is still valid (to plot data which include None values, if I am not mistaken) and shouldn't be too difficult to implement.

The horizontal/vertical line feature sounds a little more difficult to implement as I would need to plot a line between the plotting limits, which are only known after the plot is done. Extra complication: there may be double x and/or y axes active in the plot and so double plot limits. But it is a great idea so it is worth giving it a shot. Hopefully I will get an idea on how to do it properly for next version.

I will update.
Savino

P.S. you could more simply use color = 1 in instead of color = [1], in the plotting functions, when the list of colors is of only one element.

@piccolomo piccolomo changed the title Feature Request: Support non-continuous timeseries lines 2 Feature Requests: Plot Extra Grid Lines and deal with 'None' Values in Data Plot Dec 27, 2021
@piccolomo
Copy link
Owner

piccolomo commented Dec 29, 2021

Hi @NLKNguyen,

I think I solved both issues, in the new GitHub 4.1.5 version. Indeed it wasn't straightforward. Here is some code:

import pandas as pd
import plotext as plt

plt.datetime.set_datetime_form(date_form='%Y-%m-%d')

t2 = pd.date_range("2022-01-06", periods=5, freq="D") 
x2 = [plt.datetime.datetime_to_string(el) for el in t2]
y2 = pd.Series([6,7,8,9,10], index=t2).tolist()
plt.plot_date(x2, y2,  marker = "hd", color = 4)
plt.horizontal_line(8.1, color = "red")
plt.vertical_line("2022-01-07", color = "green")

plt.canvas_color(0) 
plt.axes_color(0)
plt.ticks_color("bright-yellow")
plt.show()

image

The guide on the functions is here.

I haven't properly tested the new feature and functions. If you are available and can double check, I could publish it on PyPi, after some feedback. To install from GitHub:

pip install git+https://github.com/piccolomo/plotext

Thanks for help and ideas (I have credited you here).
Savino

@NLKNguyen
Copy link
Author

OMG that's awesome. Great work! I'll surely test it out as soon as possible and let you know. Looks very promising.

@NLKNguyen
Copy link
Author

It's very robust!! None/NaN is handled cleanly. Any combination I can think of works well. The horizontal/vertical lines with overlap effect look really nice. Well done!
Being able to use the same dates for all series in a plot and can accept None/NaN is convenient when working with time-series data in tabular format like from SQL queries or Pandas Data Frames.

Thank you :)

image

import pandas as pd
import plotext as plt
import numpy as np

plt.datetime.set_datetime_form(date_form='%Y-%m-%d')

t1 = pd.date_range("2022-01-01", periods=5, freq="D")
x1 = [plt.datetime.datetime_to_string(el) for el in t1]
y1 = pd.Series([1,2,None,4,5], index=t1).tolist()
plt.plot_date(x1, y1,  marker = "hd", color=1)

t2 = pd.date_range("2022-01-01", periods=10, freq="D") 
x2 = [plt.datetime.datetime_to_string(el) for el in t2]
y2 = pd.Series([None, None, np.nan, None, None, 6,7,8,9,10], index=t2).tolist()
plt.plot_date(x2, y2,  marker = "hd", color=4)

t3 = pd.date_range("2022-01-01", periods=10, freq="D") 
x3 = [plt.datetime.datetime_to_string(el) for el in t3]
y3 = pd.Series([np.nan, np.nan, 4,5,None,7, 8, np.nan,np.nan,np.nan], index=t3).tolist()
plt.plot_date(x3, y3,  marker = "hd", color=3)

t4 = pd.date_range("2022-01-01", periods=10, freq="D") 
x4 = [plt.datetime.datetime_to_string(el) for el in t4]
y4 = pd.Series([None, 3, 5, 7, None, 3, 4, 5, None, 4], index=t4).tolist()
plt.scatter_date(x4, y4,  marker = "hd", color=6)

plt.canvas_color(0) 
plt.axes_color(0)
plt.ticks_color("bright-yellow")

plt.horizontal_line(8.1, color = "red")
plt.vertical_line("2022-01-07", color = "green")

plt.horizontal_line(5, color = "blue")
plt.vertical_line("2022-01-09", color = "yellow")

plt.show()

@piccolomo
Copy link
Owner

piccolomo commented Jan 1, 2022

Glad it worked :-),

new 4.1.5 PyPi version also available.

Thanks,
Savino

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