【问题标题】:matplotlib polar plot scientific notationmatplotlib 极坐标图科学记数法
【发布时间】:2014-09-04 23:47:50
【问题描述】:

我正在尝试使用 matplotlib 绘制极坐标图,并希望执行以下操作: a) 用科学记数法显示刻度标签 b) 以指定的间隔显示半径圆。

谁能给我建议如何使用下面的代码执行 a) 和 b)

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)

theta =[np.pi/3, np.pi/3]
theta2 =[np.pi/6, np.pi/6]

r = [0.0, 8.0e-04]
r2 = [0.0, 7.0e-04]
ax.plot(theta, r, 'r-', label ='Observed')
ax.plot(theta2, r2, 'b-', label ='Simulated')

ax.grid(True)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.25),
          ncol=3, fancybox=True, shadow=True)
plt.show()

【问题讨论】:

  • 由于ncol,您的legend 呼叫不起作用。
  • @Adobe 你是对的,图例调用并不完全正确,但是当我尝试它时,图例实际上在那里,但它在可见图形之外,如果你调整大小,你就会开始看到它这个数字很小。

标签: matplotlib polar-coordinates


【解决方案1】:

要指定径向刻度的位置,非常简单 - 你只需设置 rticks:

ax.set_rticks([0.0002, 0.0004, 0.0006, 0.0008])

格式有几个选项,这取决于您希望刻度显示的准确程度。默认格式化程序将自动切换到大数和小数的科学格式。如果你想改变它认为“小”的阈值,你可以通过修改 yaxis 格式化程序来做到这一点(y 轴是径向轴):

ax.yaxis.get_major_formatter().set_powerlimits((-3,4)) # Things smaller than 1e-3
                                                       # will be in scientific
                                                       # notation

不过,这对我来说有点搞笑,它把小“1e-4”放在了情节的左上角。

因此,如果您想强制当前的径向刻度为科学记数法,一种方法是使用您自己的格式。以下使用FormatStrFormatter

import matplotlib.ticker as ticker
# plotting code here
frmtr = ticker.FormatStrFormatter('%4.1e')
ax.yaxis.set_major_formatter(frmtr)

如果这不能完全满足您的要求,可以通过 matplotlib.ticker 获得很多选项。第二个格式化选项给了我这个:

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 2017-09-05
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多