Using subplot_kw in matplotlib to create a polar projection in subplots

时光总嘲笑我的痴心妄想 提交于 2021-01-28 08:19:30

问题


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()

My code:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=2, subplot_kw={projection:'polar'})

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.


回答1:


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))

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

{'polar': True}

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).



来源:https://stackoverflow.com/questions/50297863/using-subplot-kw-in-matplotlib-to-create-a-polar-projection-in-subplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!