我正在尝试使用matplotlib.pyplot.subplots()
创建极坐标投影,但当我尝试将字典传递给projection is not defined
matplotlib.pyplot.subplots()
我的代码:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=2, subplot_kw={projection:'polar'})
但是plt.subplot(1,1,1, projection='polar')
按预期工作。 plt.subplots()
的文档说subplot_kw
中的字典会传递给add.subplot()
,它将投影作为可选参数,所以我不确定我的错误是什么。
答案 0 :(得分:2)
您链接的文档实际上并未以这种方式显示subplot_kw
。他们展示的是调用dict()
:
fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
如果您打印subplot_kw=dict(polar=True)
的输出,则会得到:
{'polar': True}
请注意,polar
现在已成为字符串。 subplot_kw={projection:'polar'})
没有将projection
定义为字符串,它只是Python现在必须查找的变量名称(在这种情况下它不会找到它,但在其他情况下它可能会找到其他内容) )。