# Unable to maintain Aspect Ratio in 2D plots

**URL:** <https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119>\
**Category:** plotly.js\
**Created:** [May 6, 2018, 8:40pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119 "2018-05-06T20:40:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 6, 2018, 8:40pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/1 "2018-05-06T20:40:03Z")

</div>

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.

 ![Screenshot%20from%202018-05-07%2002-03-29](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/5/577f7b009729304e8740bee5ff494fde3fb33023.png)

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

 ![Screenshot%20from%202018-05-07%2002-08-21](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/ad809e14c302aaa0279f1548e9f4c4d54e6a9cec.png)

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

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [May 7, 2018, 2:28pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/2 "2018-05-07T14:28:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 8, 2018, 4:30pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/3 "2018-05-08T16:30:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [May 8, 2018, 5:46pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/4 "2018-05-08T17:46:43Z")

</div>

Have you tried updating to the latest version?

---

<div class="post-metadata">

**Author:** ![astrojuanlu](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/astrojuanlu/32/3172_2.png) [@astrojuanlu](https://community.plotly.com/u/astrojuanlu)\
**Post date:** [May 9, 2018, 1:31pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/5 "2018-05-09T13:31:42Z")

</div>

> [@etienne](#):
>
> Have you tried updating to the latest version?

Which latest version? [Consistently clarify latest release · Issue #1006 · plotly/plotly.py · GitHub](https://github.com/plotly/plotly.py/issues/1006)

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [May 9, 2018, 1:32pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/6 "2018-05-09T13:32:09Z")

</div>

the latest version on `pip`.

---

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 9, 2018, 2:48pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/7 "2018-05-09T14:48:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![astrojuanlu](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/astrojuanlu/32/3172_2.png) [@astrojuanlu](https://community.plotly.com/u/astrojuanlu)\
**Post date:** [May 10, 2018, 7:04am UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/8 "2018-05-10T07:04:13Z")

</div>

> [@shreyasbapat](#):
>
> The latest version on pip is 2.5.1.

plotly 2.6.0 was uploaded to PyPI 9 hours ago:

> **[plotly](https://pypi.org/project/plotly/2.6.0/#history)**
>
> An open-source, interactive data visualization library for Python

---

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 11, 2018, 6:50am UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/9 "2018-05-11T06:50:48Z")

</div>

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’]

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [May 11, 2018, 1:53pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/10 "2018-05-11T13:53:19Z")

</div>

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

Use `scene.aspectratio` in 3D:

> **[Plotly | Python chart attribute reference](https://plot.ly/python/reference/#layout-scene-aspectmode)**
>
> The complete reference of Plotly chart attributes for Plotly's Python library.

---

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 11, 2018, 2:29pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/11 "2018-05-11T14:29:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [May 11, 2018, 2:31pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/12 "2018-05-11T14:31:58Z")

</div>

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

Oh well, this topic was resolved in this [reply](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/2?u=etienne)

Please open another topic is your problems persist.

---

<div class="post-metadata">

**Author:** ![shreyasbapat](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/shreyasbapat/32/3174_2.png) [@shreyasbapat](https://community.plotly.com/u/shreyasbapat)\
**Post date:** [May 11, 2018, 4:30pm UTC](https://community.plotly.com/t/unable-to-maintain-aspect-ratio-in-2d-plots/10119/13 "2018-05-11T16:30:10Z")

</div>

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