简体   繁体   English

如何使用Spyder在Python中设置绘图的宽高比?

[英]How do I set the aspect ratio for a plot in Python with Spyder?

I'm brand new to Python, I just switched from Matlab. 我是Python的新手,我刚从Matlab切换。 The distro is Anaconda 2.1.0 and I'm using the Spyder IDE that came with it. 该发行版是Anaconda 2.1.0,我正在使用它附带的Spyder IDE。

I'm trying to make a scatter plot with equal ratios on the x and y axes, so that this code prints a square figure with the vertices of a regular hexagon plotted inside. 我正在尝试在x和y轴上制作具有相等比率的散点图,以便此代码打印一个正方形图形,其内部绘制正六边形的顶点。

import numpy
import cmath
import matplotlib

coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)

zeroplot = plot(real(x),imag(x), 'ro')
plt.gca(aspect='equal')
plt.show()

But plt.gca(aspect='equal') returns a blank figure with axes [0,1,0,1] , and plt.show() returns nothing. 但是plt.gca(aspect='equal')返回一个带有轴[0,1,0,1]的空白数字,而plt.show()返回任何内容。

I think the main problem is that plt.gca(aspect='equal') doesn't just grab the current axis and set its aspect ratio. 我认为主要问题是plt.gca(aspect='equal')不只是抓住当前轴并设置其纵横比。 From the documentation, ( help(plt.gca) ) it appears to create a new axis if the current one doesn't have the correct aspect ratio, so the immediate fix for this should be to replace plt.gca(aspect='equal') with: 从文档中,( help(plt.gca) )如果当前的轴没有正确的宽高比,它似乎会创建一个新轴,所以立即修复它应该是替换plt.gca(aspect='equal')用:

ax = plt.gca()
ax.set_aspect('equal')

I should also mention that I had a little bit of trouble getting your code running because you're using pylab to automatically load numpy and matplotlib functions: I had to change my version to: 我还要提一下,我在运行代码时遇到了一些麻烦,因为你正在使用pylab自动加载numpymatplotlib函数:我不得不将我的版本更改为:

import numpy
import cmath
from matplotlib import pyplot as plt

coeff = [1,0,0,0,0,0,-1]
x = numpy.roots(coeff)

zeroplot = plt.plot(numpy.real(x), numpy.imag(x), 'ro')
ax = plt.gca()
ax.set_aspect('equal')
plt.show()

People who are already comfortable with Python don't generally use Pylab, from my experience. 根据我的经验,熟悉Python的人通常不会使用Pylab。 In future you might find it hard to get help on things if people don't realise that you're using Pylab or aren't familiar with how it works. 将来,如果人们没有意识到您正在使用Pylab或者不熟悉它的工作方式,您可能会发现很难获得帮助。 I'd recommend disabling it and trying to get used to accessing the functions you need through their respective modules (eg using numpy.real instead of just real ) 我建议禁用它并尝试习惯通过各自的模块访问所需的功能(例如使用numpy.real而不仅仅是real

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM