如何在matplotlib中在极轴上使用对数刻度 [英] How to use log scale on polar axis in matplotlib

查看:47
本文介绍了如何在matplotlib中在极轴上使用对数刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个在径向轴上带有对数刻度的极坐标图,但我一直收到错误消息.一些示例代码和错误如下.在笛卡尔坐标系中似乎可以正常工作,有人知道发生了什么吗?

I am trying to create a polar plot with a log scale on the radial axis but I keep getting an error. Some example code and the error are below. It seems to work fine in Cartesian coordinates, does anyone know what's happening??

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

bazbins = np.linspace(0, 2*np.pi, 360)
fbins = np.logspace(np.log10(0.05), np.log10(0.5), 101)
theta, r = np.meshgrid(bazbins, fbins) 

# Set up plot window
fig, ax = plt.subplots(figsize=(12,9))#, subplot_kw=dict(projection='polar'))

# polar
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rscale('log')

# carthesia
#ax.set_yscale('log')

# Plot data
#ax.pcolormesh(theta, r, r)
plt.gca().invert_yaxis()
ax.contourf(theta, r, r)
ax.set_ylim((0.0, 0.5))
plt.show()

Tkinter 回调中的异常:

Exception in Tkinter callback:

Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 236, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 239, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 421, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 898, in draw
    func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1997, in draw
    a.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1045, in draw
    tick.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 239, in draw
    self.label1.draw(renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 591, in draw
    ismath=ismath)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 156, in draw_text
    return self.draw_mathtext(gc, x, y, s, prop, angle)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 145, in draw_mathtext
    x = int(x) + ox
ValueError: cannot convert float NaN to integer

推荐答案

这似乎是 matplotlib 中的一个错误.您不必在 set_rscale(或 set_yscale)之前调用 set_rlim(或 set_ylim)方法.此外,您必须以 0 或 0.0 作为下限调用 set_rlimset_ylim.下限值的其他值也将导致崩溃.其他后端也会出现此问题(我已经确认了gtkagg和pdf后端的问题).

This appears to be a bug in matplotlib. You should not have to call the set_rlim (or set_ylim) method before set_rscale (or set_yscale). In addition, you must call set_rlim or set_ylim with the 0 or 0.0 as the lower limit. Other values for the lower limit will also result in a crash. The problem occurs with other backends as well (I have confirmed the issue for the gtkagg and pdf backends).

我针对此问题提交了错误报告,可以在此处找到.如果此问题影响到您,请转到错误报告页面并发表评论,让 matplotlib 开发人员知道此问题对用户很重要.

I filed a bug report for this issue, which can be found here. If this issue is affecting you, please go to the bug report page and leave a comment to let the matplotlib developers know that this issue is important to users.

这篇关于如何在matplotlib中在极轴上使用对数刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆