Black Lives Matter. Please consider donating to Black Girls Code today.

Unable to maintain Aspect Ratio in 2D plots

While working with a Orbital Mechanics Library, “Poliastro”, we decided to shift our plotting module to plotly. So, we created a 2D plotting module using ploly which gives such outputs. The bodies that we plotted were 2D shapes. (circles) we are not able to maintain the aspect ratio of the plot.

This is a similar plot we got using matplotlib. We intend to get something like this.

This was possibly resolved in this : https://github.com/plotly/plotly.js/pull/2532
But online docs aren’t updated and we don’t know how to use it.

Here’s how: https://codepen.io/etpinard/pen/WJXdEw?editors=0010

Thanks a lot for the help @etienne
I want to ask this is for which version of plotly?
I am getting such errors while using this!
``
PlotlyDictKeyError: ‘constrain’ is not allowed in ‘xaxis’

Path To Error: [‘scene’][‘xaxis’][‘constrain’]

Valid attributes for ‘xaxis’ at path [‘scene’][‘xaxis’] under parents [‘layout’, ‘scene’]:

['autorange', 'backgroundcolor', 'calendar', 'categoryarray',
'categoryarraysrc', 'categoryorder', 'color', 'dtick',
'exponentformat', 'gridcolor', 'gridwidth', 'hoverformat', 'linecolor',
'linewidth', 'mirror', 'nticks', 'range', 'rangemode',
'separatethousands', 'showaxeslabels', 'showbackground',
'showexponent', 'showgrid', 'showline', 'showspikes', 'showticklabels',
'showtickprefix', 'showticksuffix', 'spikecolor', 'spikesides',
'spikethickness', 'tick0', 'tickangle', 'tickcolor', 'tickfont',
'tickformat', 'tickformatstops', 'ticklen', 'tickmode', 'tickprefix',
'ticks', 'ticksuffix', 'ticktext', 'ticktextsrc', 'tickvals',
'tickvalssrc', 'tickwidth', 'title', 'titlefont', 'type', 'visible',
'zeroline', 'zerolinecolor', 'zerolinewidth']

Run <xaxis-object>.help('attribute') on any of the above
‘’ is the object at [‘scene’][‘xaxis’]
``
I am using plotly version 2.5.1

Have you tried updating to the latest version?

1 Like

Which latest version? https://github.com/plotly/plotly.py/issues/1006

the latest version on pip.

The latest version on pip is 2.5.1. I tried --upgrade tag but the version remains the same! And I am using the same.
If possible can you please specify the version you are using? As then I will install the same version in my system.
Thanks for the reply!

plotly 2.6.0 was uploaded to PyPI 9 hours ago:

Still the same set of errors!
It would be great if @etienne can tell us which plotly version he is using!

See the complete Tracebacks for reference :

PlotlyDictKeyError Traceback (most recent call last)
in ()
----> 1 a = OrbitPlotter2D()
2 a.plot(earth, label=“Earth”)
3 a.plot(mars,label=“mars”)
4 a.show()

~/GitHub Clones/poliastro/src/poliastro/plotting.py in init(self)
429 yaxis=dict(
430 title=“y (km)”,
–> 431 scaleanchor=“x”,
432 ),
433 #aspectmode=“data”, # Important!

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in init(self, *args, **kwargs)
375 d = {key: val for key, val in dict(*args, **kwargs).items()}
376 for key, val in d.items():
–> 377 self.setitem(key, val, _raise=_raise)
378
379 def dir(self):

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in setitem(self, key, value, _raise)
430
431 if self._get_attribute_role(key) == ‘object’:
–> 432 value = self._value_to_graph_object(key, value, _raise=_raise)
433 if not isinstance(value, (PlotlyDict, PlotlyList)):
434 return

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in _value_to_graph_object(self, key, value, _raise)
541 # this can be None when _raise == False
542 return GraphObjectFactory.create(key, value, _raise=_raise,
–> 543 _parent=self, _parent_key=key)
544
545 def help(self, attribute=None, return_help=False):

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in create(object_name, *args, **kwargs)
797 return PlotlyList(*args, **kwargs)
798 else:
–> 799 return PlotlyDict(*args, **kwargs)
800
801

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in init(self, *args, **kwargs)
375 d = {key: val for key, val in dict(*args, **kwargs).items()}
376 for key, val in d.items():
–> 377 self.setitem(key, val, _raise=_raise)
378
379 def dir(self):

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in setitem(self, key, value, _raise)
430
431 if self._get_attribute_role(key) == ‘object’:
–> 432 value = self._value_to_graph_object(key, value, _raise=_raise)
433 if not isinstance(value, (PlotlyDict, PlotlyList)):
434 return

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in _value_to_graph_object(self, key, value, _raise)
541 # this can be None when _raise == False
542 return GraphObjectFactory.create(key, value, _raise=_raise,
–> 543 _parent=self, _parent_key=key)
544
545 def help(self, attribute=None, return_help=False):

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in create(object_name, *args, **kwargs)
797 return PlotlyList(*args, **kwargs)
798 else:
–> 799 return PlotlyDict(*args, **kwargs)
800
801

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in init(self, *args, **kwargs)
375 d = {key: val for key, val in dict(*args, **kwargs).items()}
376 for key, val in d.items():
–> 377 self.setitem(key, val, _raise=_raise)
378
379 def dir(self):

~/.local/lib/python3.6/site-packages/plotly/graph_objs/graph_objs.py in setitem(self, key, value, _raise)
426 if _raise:
427 path = self._get_path() + (key, )
–> 428 raise exceptions.PlotlyDictKeyError(self, path)
429 return
430

PlotlyDictKeyError: ‘constrain’ is not allowed in ‘xaxis’

Path To Error: [‘scene’][‘xaxis’][‘constrain’]

Valid attributes for ‘xaxis’ at path [‘scene’][‘xaxis’] under parents [‘layout’, ‘scene’]:

['autorange', 'backgroundcolor', 'calendar', 'categoryarray',
'categoryarraysrc', 'categoryorder', 'color', 'dtick',
'exponentformat', 'gridcolor', 'gridwidth', 'hoverformat', 'linecolor',
'linewidth', 'mirror', 'nticks', 'range', 'rangemode',
'separatethousands', 'showaxeslabels', 'showbackground',
'showexponent', 'showgrid', 'showline', 'showspikes', 'showticklabels',
'showtickprefix', 'showticksuffix', 'spikecolor', 'spikesides',
'spikethickness', 'tick0', 'tickangle', 'tickcolor', 'tickfont',
'tickformat', 'tickformatstops', 'ticklen', 'tickmode', 'tickprefix',
'ticks', 'ticksuffix', 'ticktext', 'ticktextsrc', 'tickvals',
'tickvalssrc', 'tickwidth', 'title', 'titlefont', 'type', 'visible',
'zeroline', 'zerolinecolor', 'zerolinewidth']

Run <xaxis-object>.help('attribute') on any of the above.
‘’ is the object at [‘scene’][‘xaxis’]

Axis constraints are only available for 2D cartesian graph (as the title of the ticket requested).

Use scene.aspectratio in 3D:

These are not 3D plots. These are 2D plots only.

Why are you setting a scene object then? They have no effect in 2D.

Oh well, this topic was resolved in this reply

Please open another topic is your problems persist.

Oh! Thanks a lot @etienne . I solved the issue! Thanks for the guidance!