matplotlib / matplotlib/mplfinance

Using with `matplotlib.testing.decorators.image_comparison`

Open
#661 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
4.4k
Forks
678
PR merge metrics
No merged PRs in 30d

Description

The problem is that the baseline images generated locally (in a Dev Container) don't match the ones on CI (GitHub Actions running the latest Ubuntu LTS) and thus my tests fail. When I compared the images I figured out that different fonts are being used: CI's Ubuntu has more fonts pre-installed than a Dev Container so the text was rendered with `Liberation Sans` instead of `DejaVu Sans`.

To quick fix it:

1. Install Liberation fonts locally
`sudo apt install fonts-liberation`
2. Drop `matplotlib` fonts cache (file location and name can be different)
`rm ~/.cache/matplotlib/fontlist-v330.json`
4. Re-generate baseline images

Also to find a solution that won't fail when some font is added/removed locally/remotely, I dug deeper and figured out that `matpltolib` has a solution for this well-known issue: [matplotlib.testing.setup()](https://github.com/matplotlib/matplotlib/blob/v3.8.3/lib/matplotlib/testing/__init__.py#L28) that calls [set_font_settings_for_testing()](https://github.com/matplotlib/matplotlib/blob/v3.8.3/lib/matplotlib/testing/__init__.py#L18) that sets `font.family` to `DejaVu Sans`.

But for `mplfinance` that makes no difference because the `font.family` gets overridden by [_apply_mpfstyle](https://github.com/matplotlib/mplfinance/blob/e0eaa075ca4dfb81246c4074d58380fdb1f3c9a8/src/mplfinance/_styles.py#L19) call. For example, the `default` style sets it to `sans-serif` (comes from `base_mpl_style='seaborn-darkgrid'`), and then nobody knows which font `maptlotlib` is going to use for rendering.

Here I came up with two solutions:

### `pytest` fixture

Pros:
- uses only public API

Cons:
- need to pass to every `.plot(...)`

```py
@pytest.fixture
def mpf_style():
return mpf.make_mpf_style(
base_mpf_style="default", rc={"font.family": "DejaVu Sans"}
)

@image_comparison(baseline_images=["test.png"])
def test(mpf_style):
mpf.plot(..., style=mpf_style)
```

### `pytest` before-all hook that mutates `default` style

Pros:
- all the existing tests left untouched
- never forget passing `style=...` in new ones

Cons:
- gets broken on the underlying `mplfinance` implementation changes

```py
def pytest_configure() -> None:
mpf._styledata.default.style["rc"].append(("font.family", "DejaVu Sans"))
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading src/mplfinance/_styles.py, especially _apply_mpfstyle, and compare it with matplotlib.testing.setup() and set_font_settings_for_testing(). Review the image-comparison tests and determine which approach keeps rendered fonts consistent between local Dev Containers and GitHub Actions without requiring each test to pass a custom style; done means the existing baseline comparisons pass in both environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, python
Domain
data-visualization, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.