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

Matplotlylib fixes #3143

Merged
merged 128 commits into from
Jun 4, 2021
Merged
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
c4068d1
assign Figure object to a variable
Pascalco Sep 11, 2020
40b9af1
WIP accelerated encoding with orjson
jonmmease Dec 5, 2020
f79e318
support fig to dict in io without cloning
jonmmease Dec 5, 2020
55720de
Merge branch 'master' into orjson_encoding
jonmmease Dec 5, 2020
7b3593a
fix clone default
jonmmease Dec 5, 2020
da915d6
Add pio.json.config object to configure default encoder
jonmmease Dec 5, 2020
7b235ef
default_encoder to default_engine
jonmmease Dec 5, 2020
7895b6a
blacken
jonmmease Dec 5, 2020
ce05a68
Handle Dash objects in to_json
jonmmease Dec 6, 2020
3ed5058
feature/include_plotlyjs uses bundles js version, new cdn-latest option
adehad Dec 8, 2020
49a777d
fix other tests that are broken by new 'cdn' option behaviour
adehad Dec 8, 2020
5553f89
make cdn url generator not test exclusive
adehad Dec 8, 2020
4ada560
add new `include_plotlyjs='cdn'`behaviour to changelog
adehad Dec 8, 2020
023ee57
fix tests after poor refactor
adehad Dec 8, 2020
12db5cb
format after using right black version
adehad Dec 8, 2020
7e4b256
format changelog entry better
adehad Dec 8, 2020
4ef6510
add JSON encoding tests
jonmmease Dec 31, 2020
101ba85
add testing of from_plotly_json
jonmmease Dec 31, 2020
67d3670
Better error message when orjson not installed and orjson engine requ…
jonmmease Dec 31, 2020
02c00da
Add orjson as optional testing dependency
jonmmease Dec 31, 2020
99ea6a1
Replace Python 3.5 CI tests with 3.8
jonmmease Dec 31, 2020
d44ec26
Try only install orjson with Python 3.6+
jonmmease Dec 31, 2020
b7d8422
Don't test orjson engine when orjson not installed
jonmmease Dec 31, 2020
ddcd6f5
Try new 3.8.7 docker image since prior guess doesn't exist
jonmmease Dec 31, 2020
33359f3
greater than!
jonmmease Dec 31, 2020
c7c1819
Bump scikit image version for Python 3.8 compatibility
jonmmease Dec 31, 2020
a8d52ab
Try to help Python 2 from getting confused about which json module to…
jonmmease Dec 31, 2020
619838f
Update pandas for Python 3
jonmmease Dec 31, 2020
7c7a272
Revert 3.8 CI updates. Too much for this PR
jonmmease Dec 31, 2020
1708703
Doh
jonmmease Dec 31, 2020
66cab10
Don't skip copying during serialization
jonmmease Dec 31, 2020
56a8945
Rename new JSON functions:
jonmmease Jan 2, 2021
0a51020
Ensure cleaned numpy arrays are contiguous
jonmmease Jan 2, 2021
4e9d64e
Use to_json_plotly in html and orca logic
jonmmease Jan 8, 2021
d4068de
Add orjson documentation dependency
jonmmease Jan 8, 2021
58b7192
Handle pandas Timestamp scalars in orjson engine
jonmmease Jan 8, 2021
974fcba
Rework date and string encoding, add and fix tests
jonmmease Jan 8, 2021
a651a63
default JSON engine to "auto"
jonmmease Jan 8, 2021
af1d88d
Fix expected JSON in html export (no spaces)
jonmmease Jan 8, 2021
1d6acc3
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 8, 2021
d51fd94
blacken
jonmmease Jan 8, 2021
042c54c
Fix expected JSON in matplotlylib test
jonmmease Jan 8, 2021
ddc1b8f
Fix expected JSON in html repr test
jonmmease Jan 8, 2021
6e6bd44
Update figure-introspection.md
nicolaskruchten Jan 12, 2021
d7928b0
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 13, 2021
76cc625
Don't drop timezones during serialization, just let Plotly.js ignore …
jonmmease Jan 13, 2021
453461d
Merge branch 'numpy_date_serialization' into orjson_encoding
jonmmease Jan 13, 2021
84ba4b5
no need to skip legacy tests now
jonmmease Jan 13, 2021
340aed3
Only try `datetime_as_string` on datetime kinded numpy arrays
jonmmease Jan 13, 2021
ef60cf5
Update configuration-options.md
nicolaskruchten Jan 18, 2021
6cea61d
Don't store object or unicode numpy arrays in figure. Coerce to lists
jonmmease Jan 21, 2021
93815c1
Try orjson encoding without cleaning first
jonmmease Jan 21, 2021
242d1fa
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Jan 21, 2021
8a3a4b3
blacken
jonmmease Jan 21, 2021
1de750a
remove scratch file
jonmmease Jan 21, 2021
81f73d5
Remove unused clone
jonmmease Jan 21, 2021
80be8bd
Remove the new "json" encoder
jonmmease Jan 22, 2021
cb54f88
Reorder dict cleaning for performance
jonmmease Jan 22, 2021
62a0ad1
Update troubleshooting.md
nicolaskruchten Feb 4, 2021
1b81b0b
fixing documents
c-chaitanya Feb 14, 2021
bb4a47c
Merge pull request #3073 from c-chaitanya/doc-prod
nicolaskruchten Feb 16, 2021
f5fbc16
document corrections
c-chaitanya Feb 17, 2021
c944597
document corrections
c-chaitanya Feb 18, 2021
249fef0
Update imshow.md
c-chaitanya Feb 18, 2021
c596d2e
Merge pull request #3079 from c-chaitanya/doc-prod
nicolaskruchten Feb 18, 2021
7b6a594
Adds function to sample colorscales
CarlAndersson Apr 7, 2021
0a0fc05
fixed test and mpl 3.4.1 compatibility
fdion Apr 9, 2021
4a250fa
added drawing of legend shapes (lines, markers)
fdion Apr 9, 2021
8627242
Merge branch 'doc-prod'
nicolaskruchten Apr 20, 2021
77b07c5
latex docs (#3157)
nicolaskruchten Apr 21, 2021
db22c9d
Merge branch 'doc-prod'
nicolaskruchten Apr 21, 2021
6387cb3
remove latex redirections
nicolaskruchten Apr 21, 2021
9377627
correct misspells, fix #3074 (#3105)
jmonticolo Apr 21, 2021
8caa7a9
Update dot-plots with mislabeled data (#3063)
acmiyaguchi Apr 22, 2021
211e1f6
Bump y18n from 3.2.1 to 3.2.2 in /packages/javascript/plotlywidget (#…
dependabot[bot] Apr 22, 2021
0142290
Bump elliptic from 6.5.3 to 6.5.4 in /packages/javascript/plotlywidge…
dependabot[bot] Apr 22, 2021
cb82e29
Bump node-fetch from 2.6.0 to 2.6.1 in /packages/javascript/plotlywid…
dependabot[bot] Apr 22, 2021
6bf5849
Replaced 'retrying' dependency with 'tenacity' in plotly package (#2911)
jmsmdy Apr 22, 2021
70877b4
Require python 3.6 and CI maintenance (#3160)
jonmmease Apr 22, 2021
5673b34
Kaleido v5 updates (#3094)
jonmmease Apr 23, 2021
f18576b
CircleCI updates: Unify requirements, drop conda, refactor to reduce …
jonmmease Apr 23, 2021
b343fcc
Fix selection of histograms with multiple traces (#2771)
meffmadd Apr 23, 2021
c60ebbc
Orca to Kaleido
chriddyp Apr 23, 2021
3fc9c82
Merge pull request #3171 from plotly/chriddyp-patch-1
chriddyp Apr 24, 2021
593672d
Merge JS package and create federated extension
Apr 8, 2021
a6c6d99
Rename and rebase
fcollonval Apr 24, 2021
38d0b36
Fix CI
fcollonval Apr 24, 2021
9c8e1f5
Handle pathlib.Path in pio (#2974)
maresb Apr 29, 2021
1fbfa0d
Merge remote-tracking branch 'origin/master' into orjson_encoding
jonmmease Apr 29, 2021
45fdece
Merge pull request #3142 from fcollonval/fcollonval/issue3036
nicolaskruchten May 5, 2021
8d665ac
old file cleanup
nicolaskruchten May 6, 2021
fa292ed
Merge pull request #3184 from plotly/rm_old_files
nicolaskruchten May 6, 2021
1b419ad
Minor fixes
CarlAndersson May 7, 2021
e774d3f
Adds tests
CarlAndersson May 7, 2021
282ed0d
Merge remote-tracking branch 'origin/master' into colorscale-sampling
CarlAndersson May 7, 2021
fb2c139
Code formatting
CarlAndersson May 7, 2021
b597748
Implements getting a colorscale by name
CarlAndersson May 7, 2021
b6e5636
Merge pull request #3186 from CarlAndersson/get-colorscale-by-name
nicolaskruchten May 7, 2021
2096d31
Update _html.py
Lix1993 May 8, 2021
db920c2
Merge pull request #3187 from Lix1993/master
nicolaskruchten May 8, 2021
41a61c8
Fix a type in configuration-options doc
xldrx May 9, 2021
c9ac32e
Merge pull request #3188 from xldrx/patch-1
nicolaskruchten May 9, 2021
10be9df
Merge branch 'master' into colorscale-sampling
CarlAndersson May 10, 2021
1b8ec25
Handle string inputs with get_colorscale
CarlAndersson May 10, 2021
2a59bc0
Merge pull request #3136 from CarlAndersson/colorscale-sampling
nicolaskruchten May 10, 2021
b8d7774
minor fix to get_colorscale
nicolaskruchten May 11, 2021
a33826d
black
nicolaskruchten May 11, 2021
5e327e7
Merge pull request #3193 from plotly/fix_sample_colorscale
nicolaskruchten May 11, 2021
4a13db6
Update figure-structure.md
nicolaskruchten May 16, 2021
5ab8da3
Update contributing.md
nicolaskruchten May 25, 2021
5301dcb
Merge pull request #2955 from plotly/orjson_encoding
nicolaskruchten May 27, 2021
fc9bc42
Fix typo in express/_doc.py
samorajp May 28, 2021
b0a5f77
Merge pull request #3208 from samorajp/patch-1
nicolaskruchten May 28, 2021
b4ecea0
Merge branch 'master' into lock_cdn_ver
nicolaskruchten May 29, 2021
388d9e4
Update creating-and-updating-figures.md
nicolaskruchten May 29, 2021
be0dbc5
Merge pull request #2961 from adehad/lock_cdn_ver
nicolaskruchten May 31, 2021
d99060f
remove 'latest' CDN URLs
nicolaskruchten May 31, 2021
9a55018
Merge pull request #3211 from plotly/rm_latest
nicolaskruchten May 31, 2021
48da65d
bumping to JS 2.0.0 RC 2
nicolaskruchten May 31, 2021
0895266
trying to fix sphinx
nicolaskruchten May 31, 2021
b03f0db
Merge pull request #3213 from plotly/jsrc2
nicolaskruchten May 31, 2021
73291c0
Merge pull request #2766 from Pascalco/patch-1
nicolaskruchten May 31, 2021
fd557e4
Merge pull request #3217 from plotly/doc-prod
nicolaskruchten May 31, 2021
5331cea
5.0.0rc1
nicolaskruchten May 31, 2021
34d61d0
changelog
nicolaskruchten Jun 1, 2021
ad2b540
fixed test and mpl 3.4.1 compatibility
fdion Apr 9, 2021
526d31c
added drawing of legend shapes (lines, markers)
fdion Apr 9, 2021
9be781c
Merge remote-tracking branch 'origin/matplotlylib_fixes' into matplot…
fdion Jun 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 115 additions & 18 deletions packages/python/plotly/plotly/matplotlylib/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,84 @@ def draw_bar(self, coll):
"assuming data redundancy, not plotting."
)

def draw_legend_shapes(self, mode, shape, **props):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand what this function does... could you please explain or provide an example pair of matplotlib/plotly figures that uses this system?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this function which is called further down on line 583 when coordinates are "axes" (plotly only handles "data"), the plots legends have no markers or lines, just text. This is why we get the warning: "Bummer! Plotly can currently only draw Line2D objects from matplotlib that are in 'data' coordinates!"

See example notebook here:

https://colab.research.google.com/drive/1-MNmqy_DeDxw8yPX0U7M9E8AEMtfIobP?usp=sharing

Matplotlib plot:
index

Plotly version:
newplot(2)

When coordinates == axes I call my new function that draws the shapes. go.layout.Shape cant handle circles, so I handle this special case, handle lines, and everything else i pass to go.layout.shape.

Result:
newplot(3)

It still is left with one warning: "I found a path object that I don't think is part of a bar chart. Ignoring."

This is due to the bounding box that in matplotlib is (now) a fancy box with curved corners. This would have to be addressed in a future PR.

"""Create a shape that matches lines or markers in legends.

Main issue is that path for circles do not render, so we have to use 'circle'
instead of 'path'.
"""
for single_mode in mode.split("+"):
x = props["data"][0][0]
y = props["data"][0][1]
if single_mode == "markers" and props.get("markerstyle"):
size = shape.pop("size", 6)
symbol = shape.pop("symbol")
# aligning to "center"
x0 = 0
y0 = 0
x1 = size
y1 = size
markerpath = props["markerstyle"].get("markerpath")
if markerpath is None and symbol != "circle":
self.msg += "not sure how to handle this marker without a valid path\n"
return
# marker path to SVG path conversion
path = ' '.join([f"{a} {t[0]},{t[1]}" for a, t in zip(markerpath[1], markerpath[0])])

if symbol == "circle":
# symbols like . and o in matplotlib, use circle
# plotly also maps many other markers to circle, such as 1,8 and p
path = None
shape_type = "circle"
x0 = -size / 2
y0 = size / 2
x1 = size / 2
y1 = size + size / 2
else:
# triangles, star etc
shape_type = "path"
legend_shape = go.layout.Shape(
type=shape_type,
xref="paper",
yref="paper",
x0=x0,
y0=y0,
x1=x1,
y1=y1,
xsizemode="pixel",
ysizemode="pixel",
xanchor=x,
yanchor=y,
path=path,
**shape
)

elif single_mode == "lines":
mode = "line"
x1 = props["data"][1][0]
y1 = props["data"][1][1]

legend_shape = go.layout.Shape(
type=mode,
xref="paper",
yref="paper",
x0=x,
y0=y+0.02,
x1=x1,
y1=y1+0.02,
**shape
)
else:
self.msg += "not sure how to handle this element\n"
return
self.plotly_fig.add_shape(legend_shape)
self.msg += " Heck yeah, I drew that shape\n"

def draw_marked_line(self, **props):
"""Create a data dict for a line obj.

This will draw 'lines', 'markers', or 'lines+markers'.
This will draw 'lines', 'markers', or 'lines+markers'. For legend elements,
this will use layout.shapes, so they can be positioned with paper refs.

props.keys() -- [
'coordinates', ('data', 'axes', 'figure', or 'display')
Expand Down Expand Up @@ -346,7 +420,7 @@ def draw_marked_line(self, **props):

"""
self.msg += " Attempting to draw a line "
line, marker = {}, {}
line, marker, shape = {}, {}, {}
if props["linestyle"] and props["markerstyle"]:
self.msg += "... with both lines+markers\n"
mode = "lines+markers"
Expand All @@ -361,23 +435,43 @@ def draw_marked_line(self, **props):
props["linestyle"]["color"], props["linestyle"]["alpha"]
)

# print(mpltools.convert_dash(props['linestyle']['dasharray']))
line = go.scatter.Line(
color=color,
width=props["linestyle"]["linewidth"],
dash=mpltools.convert_dash(props["linestyle"]["dasharray"]),
)
if props["coordinates"] == "data":
line = go.scatter.Line(
color=color,
width=props["linestyle"]["linewidth"],
dash=mpltools.convert_dash(props["linestyle"]["dasharray"]),
)
else:
shape=dict(
line = dict(
color=color,
width=props["linestyle"]["linewidth"],
dash=mpltools.convert_dash(props["linestyle"]["dasharray"])
)
)
if props["markerstyle"]:
marker = go.scatter.Marker(
opacity=props["markerstyle"]["alpha"],
color=props["markerstyle"]["facecolor"],
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
size=props["markerstyle"]["markersize"],
line=dict(
color=props["markerstyle"]["edgecolor"],
width=props["markerstyle"]["edgewidth"],
),
)
if props["coordinates"] == "data":
marker = go.scatter.Marker(
opacity=props["markerstyle"]["alpha"],
color=props["markerstyle"]["facecolor"],
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
size=props["markerstyle"]["markersize"],
line=dict(
color=props["markerstyle"]["edgecolor"],
width=props["markerstyle"]["edgewidth"],
),
)
else:
shape = dict(
opacity=props["markerstyle"]["alpha"],
fillcolor=props["markerstyle"]["facecolor"],
symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]),
size=props["markerstyle"]["markersize"],
line=dict(
color=props["markerstyle"]["edgecolor"],
width=props["markerstyle"]["edgewidth"],
),
)
if props["coordinates"] == "data":
marked_line = go.Scatter(
mode=mode,
Expand All @@ -404,6 +498,9 @@ def draw_marked_line(self, **props):
)
self.plotly_fig.add_trace(marked_line),
self.msg += " Heck yeah, I drew that line\n"
elif props["coordinates"] == "axes":
# dealing with legend graphical elements
self.draw_legend_shapes(mode=mode,shape=shape, **props)
else:
self.msg += " Line didn't have 'data' coordinates, " "not drawing\n"
warnings.warn(
Expand Down