用pyplot在不等轴上绘制圆 [英] plot circle on unequal axes with pyplot

查看:34
本文介绍了用pyplot在不等轴上绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自动缩放的pyplot生成的图形上绘制一个圆.当我跑步

I would like to plot a circle on an auto-scaled pyplot-generated graphic. When I run

ax.get_aspect()

希望找到一个可以操纵椭圆轴的值,pyplot返回:

hoping for a value with which I could manipulate the axes of a ellipse, pyplot returns:

auto

这不太有用.您建议用什么方法在不等轴的pyplot图上绘制圆?

which is less than useful. What methods would you suggest for plotting a circle on a pyplot plot with unequal axes?

推荐答案

它确实确实取决于您想要的用途.

It really does depend what you want it for.

当纵横比为自动时,在数据坐标中定义圆的问题在于,您将能够调整图形(或其窗口)的大小,并且数据比例会很好地拉伸.不幸的是,这也意味着您的圆不再是圆,而是椭圆.

The problem with defining a circle in data coordinates when aspect ratio is auto, is that you will be able to resize the figure (or its window), and the data scales will stretch nicely. Unfortunately, this would also mean that your circle is no longer a circle, but an ellipse.

有几种方法可以解决这个问题.首先,也是最简单的方法,您可以固定纵横比,然后在数据坐标中的绘图上画一个圆圈:

There are several ways of addressing this. Firstly, and most simply, you could fix your aspect ratio and then put a circle on the plot in data coordinates:

import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure()
ax = plt.axes()

ax.set_aspect(1)
theta = np.linspace(-np.pi, np.pi, 200)
plt.plot(np.sin(theta), np.cos(theta))

plt.show()

有了它,您将能够照常缩放和平移,但是形状将始终是圆形.

With this, you will be able to zoom and pan around as per usual, but the shape will always be a circle.

如果您只想在图形上放置一个圆,而与数据坐标无关,这样轴的平移和缩放不会影响圆的位置和缩放,那么您可以执行以下操作:

If you just want to put a circle on a figure, independent of the data coordinates, such that panning and zooming of an axes did not effect the position and zoom on the circle, then you could do something like:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np


fig = plt.figure()
ax = plt.axes()
patch = mpatches.Circle((325, 245), 180, alpha=0.5, transform=None)
fig.artists.append(patch)


plt.show()

这是相当先进的mpl,但是即使如此,我仍然认为它可读性强.

This is fairly advanced mpl, but even so, I think it is fairly readable.

HTH,

这篇关于用pyplot在不等轴上绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆