move late imports to top
State: closed

- [x] closes #26748
- [ ] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Visualization
description: plotting
name: Clean

Comments:
@xcz011 probably worth just simply `import pandas as pd` at the beginning and then just prefix `pd.Series` and `pd.concat`. Also, I think you're doing it, but just in case, you'll have to run `isort <the modified file>` as the imports must be in a specific order.

Did you check if in the other files inside `pandas/plotting/_matplotlib` are there similar cases?

Thanks!
> @xcz011 probably worth just simply `import pandas as pd` at the beginning and then just prefix `pd.Series` and `pd.concat`. Also, I think you're doing it, but just in case, you'll have to run `isort <the modified file>` as the imports must be in a specific order.
> 
> Did you check if in the other files inside `pandas/plotting/_matplotlib` are there similar cases?
> 
> Thanks!

@datapythonista  I found some lazy matplotlib imports in other files , such as `pandas/plotting/_matplotlib/hist.py` , do I need change those ? Thanks
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=h1) Report
> Merging [#26750](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c7748ca057cad4a80a6c4cbf8b49c5d9f3e7ea1c?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `72.22%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26750/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26750      +/-   ##
==========================================
+ Coverage    91.7%   91.72%   +0.02%     
==========================================
  Files         179      178       -1     
  Lines       50767    50774       +7     
==========================================
+ Hits        46555    46574      +19     
+ Misses       4212     4200      -12
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.32% <72.22%> (+0.03%)` | :arrow_up: |
| #single | `41.19% <0%> (-0.1%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/plotting/\_matplotlib/tools.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL3Rvb2xzLnB5) | `78.4% <100%> (ø)` | :arrow_up: |
| [pandas/plotting/\_matplotlib/timeseries.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL3RpbWVzZXJpZXMucHk=) | `66.32% <100%> (ø)` | :arrow_up: |
| [pandas/plotting/\_matplotlib/style.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL3N0eWxlLnB5) | `85.96% <100%> (ø)` | :arrow_up: |
| [pandas/plotting/\_matplotlib/misc.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL21pc2MucHk=) | `38.93% <50%> (+0.9%)` | :arrow_up: |
| [pandas/plotting/\_matplotlib/boxplot.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL2JveHBsb3QucHk=) | `75.59% <66.66%> (+0.48%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/indexes/interval.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9pbnRlcnZhbC5weQ==) | `96.11% <0%> (-0.32%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.88% <0%> (-0.12%)` | :arrow_down: |
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.37% <0%> (ø)` | :arrow_up: |
| [pandas/core/indexes/timedeltas.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy90aW1lZGVsdGFzLnB5) | `90.96% <0%> (ø)` | :arrow_up: |
| ... and [9 more](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=footer). Last update [c7748ca...d95a9bf](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=h1) Report
> Merging [#26750](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c7748ca057cad4a80a6c4cbf8b49c5d9f3e7ea1c?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26750/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26750      +/-   ##
==========================================
- Coverage    91.7%   91.69%   -0.01%     
==========================================
  Files         179      179              
  Lines       50767    50764       -3     
==========================================
- Hits        46555    46550       -5     
- Misses       4212     4214       +2
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.29% <100%> (ø)` | :arrow_up: |
| #single | `41.2% <0%> (-0.09%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/plotting/\_matplotlib/boxplot.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19tYXRwbG90bGliL2JveHBsb3QucHk=) | `75.71% <100%> (+0.59%)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26750/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `96.88% <0%> (-0.12%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=footer). Last update [c7748ca...256a536](https://codecov.io/gh/pandas-dev/pandas/pull/26750?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@xcz011 all those were lazy to avoid requiring matplotlib if plotting wasn't used. Now that is managed in `pandas/plotting/_core.py::_get_plot_backend` that doesn't load `pandas/plotting/_matplotlib` until the user plots something.

So, you can move the other imports to the top too, that's not required and should improve readability of the file.
> @xcz011 all those were lazy to avoid requiring matplotlib if plotting wasn't used. Now that is managed in `pandas/plotting/_core.py::_get_plot_backend` that doesn't load `pandas/plotting/_matplotlib` until the user plots something.
> 
> So, you can move the other imports to the top too, that's not required and should improve readability of the file.

@datapythonista Thanks for this detail explanation! I just made change to most of files in `_matplotlib`, except `core.py`. I not fully sure which do I need move those lazy import to the top too ? Thanks !  
looks fine, these are isorted by default right? @WillAyd 
isort doesn't make mods by default in our CI but would fail it if out of order, so this looks good
Thanks @xcz011 !