英文:
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()
我期望获得这个图:
但实际上出现了以下错误:
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:
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之后:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论