本文介绍了在matplotlib中使用subplot_kw在子图中创建极坐标投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib.pyplot.subplots() 创建极坐标投影,但是当我尝试传递字典时出现错误 projection is not defined matplotlib.pyplot.subplots()

I'm trying to create a polar projection using matplotlib.pyplot.subplots() but I get the error projection is not defined when I try to pass a dictionary to 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() ,它将投影作为可选参数,所以我不确定我的错误是什么.

However plt.subplot(1,1,1, projection='polar') works as expected. The documentation for plt.subplots() says that the dictionary in subplot_kw will be passed to add.subplot() which takes projection as a optional parameter so I'm not sure what my mistake is.

推荐答案

您链接的文档实际上并未显示 subplot_kw 以这种方式使用.他们显示的是调用 dict():

The docs that you linked don't actually show subplot_kw being used in that way. What they show is calling dict():

fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))

如果打印 subplot_kw = dict(polar = True)的输出,则会得到:

If you print the output of subplot_kw=dict(polar=True), you get:

{'polar': True}

请注意, polar 现在已成为字符串. subplot_kw = {projection:'polar'})并未将 projection 定义为字符串,它只是Python现在必须查找的变量名(并且不会在这种情况下找到它,但在其他情况下可能会找到其他东西.

Notice that polar has now become a string. subplot_kw={projection:'polar'}) does not define projection as a string, it's just a variable name that Python now has to look up (and it won't find it in this case, but it may find something else in other cases).

这篇关于在matplotlib中使用subplot_kw在子图中创建极坐标投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 12:34