非正交图中的等值线图不旋转

发布于 2025-01-10 19:55:20 字数 1840 浏览 0 评论 0原文

在这里您可以看到一个示例,其中我尝试在现有图中添加等高线图。我想用 grid_helper 旋转 y 轴的“水平”线。在第一张图中,您可以看到 y 轴的水平线在没有 grid_helper 的情况下是正交的。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.grid_helper_curvelinear import GridHelperCurveLinear
from mpl_toolkits.axisartist import Subplot

# Define the range of x and y
x = np.linspace(0, 30, 100)
y = np.linspace(0, 30, 100)

# Generate a meshgrid
X, Y = np.meshgrid(x, y)

# Define z as a function of x and y
def z(x, y):
    return  x + y

# Use the grid_helper tool in order to turn the y axis from orthogonal to non-orthogonal
def tr(x, y):
    x, y = np.asarray(x), np.asarray(y)
    return x, -2.5 * x + y

def inv_tr(x, y):
    x, y = np.asarray(x), np.asarray(y)
    return 4 / 3 * x + -1 / 3 * y, x + 4 / 3 * y

grid_helper = GridHelperCurveLinear((tr, inv_tr))

# Adding a subplot and create a contour plot with X,Y and z(X,Y)
# This would be the contour plot in the figure with an orthogonal y-axis
fig = plt.figure(figsize=(25, 15))
ax1 = Subplot(fig, 1, 1, 1)

fig.add_subplot(ax1)
ax1.grid(visible=True, which="both", axis="y", linewidth=1)

ax1.contour(X, Y, z(X, Y))

plt.show()

创建的图应如下所示:

[正交系统中的等高线图]

现在我想要通过在我的子图中实现网格助手来在非正交系统中添加等高线图。

最后几行应如下所示:

fig = plt.figure(figsize=(25, 15))

#Here I implement the grid_helper in the subplot
ax1 = Subplot(fig, 1, 1, 1, grid_helper=grid_helper)

fig.add_subplot(ax1)
ax1.grid(visible=True, which="both", axis="y", linewidth=1)

ax1.contour(X, Y, z(X, Y))

plt.show()

第二个图如下所示:

[非-prthogonal system]

如您所见,grid_helper 对 y 轴的“水平”线有影响,但不会旋转等高线图的线。我的问题是,是否有任何方法可以像我对 y 轴线所做的那样旋转等高线图线。

Here you can see an example where i try to add a contour plot in an existing plot. I want to rotate the the "horizontal" lines of the y-axis with the grid_helper. In the first image you can see that the horizontal lines of the y-axis are orthogonal without the grid_helper.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.grid_helper_curvelinear import GridHelperCurveLinear
from mpl_toolkits.axisartist import Subplot

# Define the range of x and y
x = np.linspace(0, 30, 100)
y = np.linspace(0, 30, 100)

# Generate a meshgrid
X, Y = np.meshgrid(x, y)

# Define z as a function of x and y
def z(x, y):
    return  x + y

# Use the grid_helper tool in order to turn the y axis from orthogonal to non-orthogonal
def tr(x, y):
    x, y = np.asarray(x), np.asarray(y)
    return x, -2.5 * x + y

def inv_tr(x, y):
    x, y = np.asarray(x), np.asarray(y)
    return 4 / 3 * x + -1 / 3 * y, x + 4 / 3 * y

grid_helper = GridHelperCurveLinear((tr, inv_tr))

# Adding a subplot and create a contour plot with X,Y and z(X,Y)
# This would be the contour plot in the figure with an orthogonal y-axis
fig = plt.figure(figsize=(25, 15))
ax1 = Subplot(fig, 1, 1, 1)

fig.add_subplot(ax1)
ax1.grid(visible=True, which="both", axis="y", linewidth=1)

ax1.contour(X, Y, z(X, Y))

plt.show()

The created plot should look like this:

[Contour plot in the orthogonal system]

Now i want to add the contour plot in the non-orthogonal system by implementing the grid helper in my Subplot.

the last few lines should look like this:

fig = plt.figure(figsize=(25, 15))

#Here I implement the grid_helper in the subplot
ax1 = Subplot(fig, 1, 1, 1, grid_helper=grid_helper)

fig.add_subplot(ax1)
ax1.grid(visible=True, which="both", axis="y", linewidth=1)

ax1.contour(X, Y, z(X, Y))

plt.show()

The second plot looks like this:

[Contour plot in the non-prthogonal system]

As you can see, the grid_helper had an effect on the "horizontal" lines of the y-axis but didn't rotate the lines of the contour plot. My Question is, if there is any way to rotate the contour plot lines like I did with the lines of the y-axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文