matplotlib / matplotlib/mplfinance

Bug Report: Errors occur for NaN columns.

Open Beginner friendly
#672 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

# issue case
When animating over a limited visual range, as in this example, the possibility arises that the signal columns are all NaN.

https://github.com/matplotlib/mplfinance/assets/37892107/a5afea85-57ce-4282-ad7c-955240c54787

# issue
mplfinance\plotting.py", line 1108, in _addplot_columns
ymhi = math.log(max(math.fabs(np.nanmax(yd)),1e-7),10)
This Numpy code generates an error.
"ValueError: zero-size array to reduction operation maximum which has no identity"

# Error reproduction
``` python
import datetime

import numpy as np
import pandas as pd
import mplfinance as mpf

# ランダムなOHLCデータを生成する関数
def generate_sample_ohlc_data(n=10):
# 現在の日付を取得
base_date = datetime.datetime.now()
# 日付のリストを生成
dates = [base_date - datetime.timedelta(days=i) for i in range(n)]
dates.reverse()

# ランダムな価格データを生成
rand = np.random.default_rng().uniform
open = [100]
high = [100.02]
low = [100.02]
close = [100.05]
for i in range(n-1):
open.append(close[i])
close.append(open[-1] + rand(-0.1, 0.1))
high.append(max(open[-1],close[-1]) + rand(0, 0.05))
low.append(min(open[-1],close[-1]) - rand(0, 0.05))

# DataFrameに格納
ohlc_data = pd.DataFrame({
"date": dates,
"open": open,
"high": high,
"low": low,
"close": close
})

return ohlc_data.set_index("date", drop=True)

period_range = 50
df = generate_sample_ohlc_data(period_range)
df["signal"] = np.nan
print(df)

some_signal = mpf.make_addplot(df["signal"])
mpf.plot(df, type='candle', addplot=some_signal) # ValueError
```

Currently, we have worked around this by doing NaN pre-checking on the user side, but it would be great if the library could handle this.

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in the issue and inspect mplfinance/plotting.py at _addplot_columns, especially the np.nanmax call around line 1108. Confirm the failure with an addplot column containing only NaN values; done means plotting that data no longer raises the zero-size reduction ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.