使用 matplotlib,有没有办法创建基本不等式的简单二维图

Using matplotlib, is there a way to create simple 2d plots of basic inequalities

如何创建基本不等式的简单二维图。结果是一条带编号的行,其中有一行或两行重叠。一条或多条线将以箭头、实心圆或空心圆开始或结束。

您可以使用的一些技巧:

  • 创建一个情节,将x-axis设置在中心,隐藏所有其他刺
  • 使用线图创建线条和箭头
  • 设置 xticks 'inout' 并使刻度更长
  • 对特殊点使用填充标记;给定空心与填充效果,填充颜色可以是白色或绿松石色
  • 设置 zorders 使线条位于 x 轴的顶部
  • 如果你想要多个这样的轴,使用plt.subplots(nrows=...),相应地设置figsize

这里有一些代码可以帮助您入门。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1)
x0 = -5
x1 = 5
p = 3
ax.set_ylim(-1, 1)
ax.set_xlim(x0 - 0.4, x1 + 0.4)
ax.set_xticks(range(x0, x1 + 1))
ax.set_yticks([])
ax.tick_params(axis='x', direction='inout', length=10)
ax.spines['bottom'].set_position('zero')
ax.spines['bottom'].set_zorder(0)
for dir in ['left', 'right', 'top']:
    ax.spines[dir].set_visible(False)
ax.plot([x0 - 0.4, p], [0, 0], color='turquoise', lw=1)
ax.plot([x0 - 0.2, x0 - 0.4, x0 - 0.2], [0.2, 0, -0.2], color='turquoise', lw=1)
ax.plot([x1 + 0.2, x1 + 0.4, x1 + 0.2], [0.2, 0, -0.2], color='black', lw=1)
ax.plot(p, 0, linestyle='', marker='o', fillstyle='full', markerfacecolor='white', markeredgecolor='turquoise',
        markersize=5, lw=2, zorder=3)
ax.set_aspect('equal')
plt.show()

是的,有。将 matplotlib 添加到您的 python 发行版中,然后将其导入您的代码中。我通常在导入时使用以下内容。 import matplotlib.pyplot as plt. Then I can use the following line also in my python code: plt.plot(x, y)` 这将为您提供已定义值的简单 x,y 图。

在 import matplotlib.pyplot as plt 行中,我将 maplotlib 作为 plt 加载。这使您可以将其称为 plt。 x-axis 可以由用户定义为一组值,或者您可以使用类似 x = np.linspace(0, 20, 200) 的东西生成 x-values,其中 linspace 是生成等距点的函数。 0 和 20 指的是 xmin 和 xmax 值,而 200 是生成的 x-values 个数。

你也可以简单地做

x = (1,3,5,9,11,13)
y = (1,9,25,81,121,169)

和plt.plot(x,y)