使用matplotlib.patheffects中的withTickedStroke来处理可行区域

huangapple go评论72阅读模式
英文:

Using withTickedStroke from matplotlib.patheffects for feasible regions

问题

我一直在尝试运行 matplotlib 中用于轮廓优化问题解决方案的 演示,但 patheffects 并没有与链接中所述的相同的属性。

演示的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import patheffects

fig, ax = plt.subplots(figsize=(6, 6))

nx = 101
ny = 105

# 设置调查向量
xvec = np.linspace(0.001, 4.0, nx)
yvec = np.linspace(0.001, 4.0, ny)

# 设置调查矩阵。设计盘加载和齿轮比。
x1, x2 = np.meshgrid(xvec, yvec)

# 评估一些要绘制的内容
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
g1 = -(3*x1 + x2 - 5.5)
g2 = -(x1 + 2*x2 - 4.5)
g3 = 0.8 + x1**-3 - x2

cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
                  colors='black')
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)

cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
plt.setp(cg1.collections,
         path_effects=[patheffects.withTickedStroke(angle=135)])

cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
plt.setp(cg2.collections,
         path_effects=[patheffects.withTickedStroke(angle=60, length=2)])

cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
plt.setp(cg3.collections,
         path_effects=[patheffects.withTickedStroke(spacing=7)])

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

plt.show()

我期望获得这个图:

使用matplotlib.patheffects中的withTickedStroke来处理可行区域

但实际上出现了以下错误:

AttributeError: 模块 'matplotlib.patheffects' 没有属性 'withTickedStroke'

在源代码中搜索了一下,似乎存在某个版本的 TickedStroke,但无法直接作为 patheffects 的属性访问,我无法使它们工作。任何帮助都将不胜感激!

英文:

I've been trying to run the demo for contouring optimization problems solutions in matplotlib, but patheffects doesn't have the same attributes as the ones stated in the link.

The code of the demo is the following:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import patheffects

fig, ax = plt.subplots(figsize=(6, 6))

nx = 101
ny = 105

# Set up survey vectors
xvec = np.linspace(0.001, 4.0, nx)
yvec = np.linspace(0.001, 4.0, ny)

# Set up survey matrices.  Design disk loading and gear ratio.
x1, x2 = np.meshgrid(xvec, yvec)

# Evaluate some stuff to plot
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
g1 = -(3*x1 + x2 - 5.5)
g2 = -(x1 + 2*x2 - 4.5)
g3 = 0.8 + x1**-3 - x2

cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
                  colors='black')
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)

cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
plt.setp(cg1.collections,
         path_effects=[patheffects.withTickedStroke(angle=135)])

cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
plt.setp(cg2.collections,
         path_effects=[patheffects.withTickedStroke(angle=60, length=2)])

cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
plt.setp(cg3.collections,
         path_effects=[patheffects.withTickedStroke(spacing=7)])

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

plt.show()

I expected to get this plot:

使用matplotlib.patheffects中的withTickedStroke来处理可行区域

but the following error arised instead,

AttributeError: module 'matplotlib.patheffects' has no attribute 'withTickedStroke'

Searching in the source code some version of TickedStroke exists, but aren't accesible directly as an attribute of patheffects and I can't get them to work. Any help is appreciated!

答案1

得分: 1

升级到最新版本的matplotlib(至少3.4)。您的版本太旧(matplotlib < 3.4):

pip install -U matplotlib

在3.4之前:

...
AttributeError: module 'matplotlib.patheffects' has no attribute 'withTickedStroke'

在3.4之后:

使用matplotlib.patheffects中的withTickedStroke来处理可行区域

英文:

Upgrade to the last version of matplotlib (or at least 3.4). You version is too old (matplotlib < 3.4):

&gt;&gt;&gt; pip install -U matplotlib

Before 3.4:

...
AttributeError: module &#39;matplotlib.patheffects&#39; has no attribute &#39;withTickedStroke&#39;

After 3.4:

使用matplotlib.patheffects中的withTickedStroke来处理可行区域

huangapple
  • 本文由 发表于 2023年5月28日 13:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定