Repository: optuna/optuna

Issue:
The objective range of Matplotlib contour plot has a different numerical range from Plotly backend
<!-- Please write a clear and concise description of what the bug is. -->

## Expected behavior

The range of objective  in `optuna.visualization.matplotlib.plot_contour` should have a similar range to `optuna.visualization.plot_contour`.


## Environment

- Optuna version: 2.7.0
- Python version: 3.8.8
- OS: Mac
- (Optional) Other libraries and their versions:
    - matplotlib: 3.3.4
    - Plotly: 4.11.0

## Error messages, stack traces, or logs
The matplotlib range is from `-60 000` to `360 000` as in the following figure.

![download (2)](https://user-images.githubusercontent.com/7121753/114876635-79e04f00-9e39-11eb-97c6-3b7b16e89456.png)

On the other hand, Plotly's range is from `0` to `9 000`. That is supposed to be true.

![newplot (2)](https://user-images.githubusercontent.com/7121753/114876650-7baa1280-9e39-11eb-9c3e-554d16ed2496.png)


## Steps to reproduce

1. Please run a following example code blocks


## Reproducible examples (optional)

```python
import optuna


def objective(trial):
    x = trial.suggest_uniform('x', -100, 100)
    y = trial.suggest_categorical('y', list(range(-10, 10)))
    z = trial.suggest_uniform('z', -100, 100)
    return x ** 2 + y - z

study = optuna.create_study(sampler=optuna.samplers.TPESampler(seed=7))
study.optimize(objective, n_trials=200)

optuna.visualization.matplotlib.plot_contour(study, params=['y', 'z'])
optuna.visualization.plot_contour(study, params=['y', 'z']) # plotly
```




Discussion:
I think the incorrect color bar comes from the interpolation of `z` values:
https://github.com/optuna/optuna/blob/ef0a7977a78a2ba7de20b652ce3c954828d461d1/optuna/visualization/matplotlib/_contour.py#L320-L325

If we set another interpolation method such as `linear`, the colormap looks more accurate:
![download (6)](https://user-images.githubusercontent.com/7121753/115153720-5e33ad80-a0b2-11eb-89c2-896dcec508cc.png)

I also found matplotlib's linear interpolation method for grid data when I investigated this issue. It might be useful to interpolate `z`.
https://matplotlib.org/stable/gallery/images_contours_and_fields/irregulardatagrid.html
I would like to point out the different color scale between plotly and matplotlib:

- plotly: white (smaller objective) -- dark-blue (higher objective)
- matplotlib: white (higher objective) -- dark blue (smaller objective)
This issue has not seen any recent activity.
I would like to work on this issue.
@01-vyom Great! please go ahead! 

Let's clarify how to resolve this issue.

I think you can send two pull requests: one is to fix the numerical range and the other is to fix the inversed colour scale as in https://github.com/optuna/optuna/issues/2595#issuecomment-822151302. You can choose either one or both!

In terms of the numerical range, ideally, we would like to see a similar interpolation to the interpolation used in Plotly for consistency. But if it is difficult, it is fine to use another interpolation method like `linear`.

Ok, I can work on both things one by one.

For the color scale, we can directly do `cmap.reversed()`.

For the numeric range, I will work on it and get back to you.
I was reading the provided [link](https://matplotlib.org/stable/gallery/images_contours_and_fields/irregulardatagrid.html) for interpolation of irregular space data and tried the [CubicInterpolation](https://matplotlib.org/stable/api/tri_api.html#matplotlib.tri.CubicTriInterpolator) code [interpolating z], still got the same results (incorrect numeric range).  The [Linear interpolation](https://matplotlib.org/stable/api/tri_api.html#matplotlib.tri.LinearTriInterpolator) and the option `linear` in the griddata function works in the same way, so we can just use that if we go for linear interpolation. Let me know your thoughts...
Brilliant! Thank you for sharing your investigation. It sounds great -- reviewers can understand the change in the pull request smoothly.

Resolving patch:
diff --git a/optuna/visualization/matplotlib/_contour.py b/optuna/visualization/matplotlib/_contour.py
--- a/optuna/visualization/matplotlib/_contour.py
+++ b/optuna/visualization/matplotlib/_contour.py
@@ -315,13 +315,13 @@ def _calculate_griddata(
         else:
             yi = np.linspace(y_values_min, y_values_max, contour_point_num)
 
-        # Interpolate z-axis data on a grid with cubic interpolator.
+        # Interpolate z-axis data on a grid with linear interpolator.
         # TODO(ytknzw): Implement Plotly-like interpolation algorithm.
         zi = griddata(
             np.column_stack((x_values, y_values)),
             z_values,
             (xi[None, :], yi[:, None]),
-            method="cubic",
+            method="linear",
         )
 
     return (


Test patch: