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

Implement (DataFrame|Series).plot.hist in plotly #1999

Merged
merged 4 commits into from
Jan 13, 2021

Conversation

HyukjinKwon
Copy link
Member

@HyukjinKwon HyukjinKwon commented Jan 8, 2021

This PR implements (DataFrame|Series).plot.hist in plotly:

This can be tested via: https://mybinder.org/v2/gh/HyukjinKwon/koalas/plotly-histogram?filepath=docs%2Fsource%2Fgetting_started%2F10min.ipynb

Example:

# Koalas
import databricks.koalas as ks
ks.options.plotting.backend = "plotly"
kdf = ks.DataFrame({
    'a c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 50],
    'b': [2, 3, 4, 5, 7, 9, 10, 15, 10, 20, 20]
})
(kdf + 100).plot.hist()

# pandas
import pandas as pd
pd.options.plotting.backend = "plotly"
pdf = pd.DataFrame({
    'a c': [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 50],
    'b': [2, 3, 4, 5, 7, 9, 10, 15, 10, 20, 20]
})
(pdf + 100).plot.hist()

Screen Shot 2021-01-12 at 10 12 47 PM

Screen Shot 2021-01-12 at 10 12 52 PM

NOTE that the output is a bit different because:

  • We use Spark for histogram calculation and it's a bit different from pandas'
  • Histogram plot in plotly cannot be directly used in our case but should work around by leveraging bar charts because we don't use plotly to calculate histogram, see https://plotly.com/python/histograms/.

@HyukjinKwon HyukjinKwon marked this pull request as draft January 8, 2021 05:52
@codecov-io
Copy link

codecov-io commented Jan 8, 2021

Codecov Report

Merging #1999 (1c0ba08) into master (ea6ad98) will increase coverage by 0.01%.
The diff coverage is 98.26%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1999      +/-   ##
==========================================
+ Coverage   94.53%   94.55%   +0.01%     
==========================================
  Files          50       50              
  Lines       10995    11086      +91     
==========================================
+ Hits        10394    10482      +88     
- Misses        601      604       +3     
Impacted Files Coverage Δ
databricks/koalas/plot/core.py 90.82% <88.23%> (-3.15%) ⬇️
databricks/koalas/plot/matplotlib.py 92.62% <100.00%> (ø)
databricks/koalas/plot/plotly.py 97.67% <100.00%> (+2.93%) ⬆️
databricks/koalas/tests/plot/test_frame_plot.py 100.00% <100.00%> (ø)
...bricks/koalas/tests/plot/test_frame_plot_plotly.py 100.00% <100.00%> (ø)
databricks/koalas/tests/plot/test_series_plot.py 100.00% <100.00%> (ø)
...ricks/koalas/tests/plot/test_series_plot_plotly.py 97.64% <100.00%> (+1.03%) ⬆️
databricks/koalas/series.py 96.72% <0.00%> (-0.03%) ⬇️
databricks/koalas/indexes.py 96.71% <0.00%> (+0.08%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ea6ad98...1c0ba08. Read the comment docs.

@HyukjinKwon HyukjinKwon force-pushed the plotly-histogram branch 9 times, most recently from 4bb1a2b to 0eef796 Compare January 12, 2021 04:57
@HyukjinKwon HyukjinKwon marked this pull request as ready for review January 12, 2021 07:28
@HyukjinKwon HyukjinKwon marked this pull request as draft January 12, 2021 10:14
@HyukjinKwon HyukjinKwon force-pushed the plotly-histogram branch 8 times, most recently from 77566f8 to 63f23b8 Compare January 12, 2021 13:30
@@ -338,31 +344,41 @@ def _get_plot_backend(backend=None):

KoalasPlotAccessor._backends["matplotlib"] = module

if backend == "plotly":
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I avoided to monkey patch plotly. I will need to refactor codes here in another PR .. here needs some refactoring.

@@ -16,7 +16,7 @@

from distutils.version import LooseVersion

import matplotlib
import matplotlib as mat
Copy link
Member Author

@HyukjinKwon HyukjinKwon Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is due to the conflict when you import, from databricks.koalas.plot import matplotlib
As a module databricks.koalas.plot.matplotlib vs matplotlib library imported at databricks.koalas.plot. I will fix it back in the next PR that refactors.

@HyukjinKwon HyukjinKwon marked this pull request as ready for review January 12, 2021 13:35
@HyukjinKwon HyukjinKwon force-pushed the plotly-histogram branch 2 times, most recently from 41e542f to 4ae387a Compare January 12, 2021 14:29
@HyukjinKwon HyukjinKwon force-pushed the plotly-histogram branch 2 times, most recently from 9575d6d to 42a04a2 Compare January 12, 2021 16:17
Copy link
Collaborator

@ueshin ueshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good.

databricks/koalas/plot/core.py Outdated Show resolved Hide resolved
Comment on lines +21 to +32
def plot(data, kind, **kwargs):
import plotly

return plot
# Koalas specific plots
if kind == "pie":
return plot_pie(data, **kwargs)
if kind == "hist":
# Note that here data is a Koalas DataFrame or Series unlike other type of plots.
return plot_histogram(data, **kwargs)

# Other plots.
return plotly.plot(data, kind, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 👍

@HyukjinKwon HyukjinKwon merged commit 3ce2d87 into databricks:master Jan 13, 2021
ueshin pushed a commit that referenced this pull request Jan 15, 2021
This PR implements Series.plot.box with plotly. Note that DataFrame.plot.box is not already supported in Koalas yet.
This can be tested via the link: https://mybinder.org/v2/gh/HyukjinKwon/koalas/plot-box-ser?filepath=docs%2Fsource%2Fgetting_started%2F10min.ipynb

Note that you should manually install plotly to test with mybinder above:

```
%%bash
pip install plotly
```

Example:

```python
# Koalas
from databricks import koalas as ks
ks.options.plotting.backend = "plotly"
kdf = ks.DataFrame({"a": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 50],}, index=[0, 1, 3, 5, 6, 8, 9, 9, 9, 10, 10])
kdf.a.plot.box()

# pandas
import pandas as pd
pd.options.plotting.backend = "plotly"
pdf = pd.DataFrame({"a": [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 50],}, index=[0, 1, 3, 5, 6, 8, 9, 9, 9, 10, 10])
pdf.a.plot.box()
```

![Screen Shot 2021-01-14 at 6 56 19 PM](https://user-images.githubusercontent.com/6477701/104575700-acdc4080-569a-11eb-8d55-0ac3db800ddd.png)
![Screen Shot 2021-01-14 at 6 56 24 PM](https://user-images.githubusercontent.com/6477701/104575705-ad74d700-569a-11eb-9b7c-a37e04f77ec7.png)

For the same reason as #1999, the output is slightly different from pandas'.
I referred to "Box Plot With Precomputed Quartiles" in https://plotly.com/python/box-plots/
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

Successfully merging this pull request may close these issues.

3 participants