简体   繁体   English

Python - plot 等式: x + abs(x) = y + abs(y)

[英]Python - plot the equation: x + abs(x) = y + abs(y)

Why is this program plotting the wrong thing?为什么这个程序绘制错误的东西?
I mean... why is the ray (x=0, y<=0) missing?我的意思是……为什么射线(x=0, y<=0)不见了? Isn't this a bug?这不是一个错误吗?

import matplotlib.pyplot
from numpy import arange
from numpy import meshgrid

delta = 0.025
xrange = arange(-20.0, 20.0, delta)
yrange = arange(-20.0, 20.0, delta)
X, Y = meshgrid(xrange,yrange)

# F is one side of the equation, G is the other
F = Y + abs(Y)
G = X + abs(X)

matplotlib.pyplot.contour(X, Y, (F - G), [0])
matplotlib.pyplot.show()

阴谋

The correct plot should be something like this below.正确的 plot 应该如下所示。
How can I achieve something like that in Python?如何在 Python 中实现类似的功能?

情节2

To draw implicit equations (or inequalities), sympy 's plot_implicit might come in handy.要绘制隐式方程(或不等式), sympyplot_implicit可能会派上用场。 Just calling plot_implicit(Eq(x+Abs(x), y+Abs(y))) draws the equation with some default bounds.只需调用plot_implicit(Eq(x+Abs(x), y+Abs(y)))绘制具有一些默认边界的方程。

Unfortunately, sympy's plotting is quite primitive in options, and standard matplotlib functions are hard to combine.不幸的是,sympy 的绘图选项非常原始,标准的 matplotlib 函数很难组合。 Also, part of the documentation is still talking about "pyglet plotting" which isn't really supported anymore.此外,部分文档仍在谈论不再真正支持的“pyglet 绘图”。

from sympy import Abs, Eq, plot_implicit
from sympy.abc import x, y

# plot_implicit(Eq(x+Abs(x), y+Abs(y)))
plot_implicit(Eq(x+Abs(x), y+Abs(y)), (x, -20, 20), (y, -20, 20))

结果图

Using some tricks from this post , a sympy plot can be moved to matplotlib axes.使用这篇文章中的一些技巧,可以将 sympy plot 移动到 matplotlib 轴。 There, the rotation of the y label and the line thickness can be changed (it seems just changing the width isn't enough, also the edgecolor needs to be set afterwards).在那里,y label 的旋转和线的粗细可以改变(看起来只是改变宽度是不够的,还需要在之后设置edgecolor)。

from sympy import Abs, Eq, plot_implicit
from sympy.abc import x, y
from matplotlib import pyplot as plt
import matplotlib as mpl

def move_sympyplot_to_axes(p, ax):
    backend = p.backend(p)
    backend.ax = ax
    # backend.process_series()
    backend._process_series(backend.parent._series, ax, backend.parent)
    backend.ax.spines['right'].set_color('none')
    backend.ax.spines['bottom'].set_position('zero')
    backend.ax.spines['top'].set_color('none')
    plt.close(backend.fig)

p1 = plot_implicit(Eq(x+Abs(x), y+Abs(y)), (x, -20, 20), (y, -20, 20), show=False)

fig, ax = plt.subplots()
move_sympyplot_to_axes(p1, ax)
plt.setp(ax.yaxis.get_label(), 'rotation', 0)
ax.get_children()[0].set_linewidth(2)
ax.get_children()[0].set_edgecolor('deepskyblue')

plt.show()

新情节

This might be a cheat, but plt.fill_between() can be used to fill the area between two lines (see doc ).这可能是作弊,但plt.fill_between()可用于填充两行之间的区域(请参阅doc )。

import matplotlib.pyplot as plt

q = 50
x = [-q, 0, 0, q]
y1 = [0, 0 , 0, q]
y2 = [-q, -q, 0 , q]

fig, ax = plt.subplots()

ax.plot(x, y1, color='C0')
ax.plot(x, y2, color='C0')
ax.fill_between(x, y1, y2, alpha=0.2, color='C0')

ax.set_xlim(-20,20)
ax.set_ylim(-20,20)
plt.show()

在此处输入图像描述

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

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