【问题标题】:How to place xaxis grid over spectrogram in Python?如何在 Python 中将 xaxis 网格放置在频谱图上?
【发布时间】:2015-10-15 06:21:15
【问题描述】:

我有以下图表,它提供了压力信号的频谱图以及放置在其上的信号以进行比较。我可以在频谱图上绘制 y 轴网格,但无法在其上放置 x 轴网格。

用于生成频谱图的数据在here可用。

可重现的代码

from __future__ import division
from matplotlib import ticker as mtick
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('pressure.dat', skiprows = 1, delimiter = '\t')
pressure = data[:, 1]
theta = data[:, 0]


with PdfPages('Spectorgram of cylinder pressure.pdf') as spectorgram_pressure:
    _spectorgram_pressure_vs_frequency_ = plt.figure(figsize=(5.15, 5.15))
    _spectorgram_pressure_vs_frequency_.clf()
    spectorgram_pressure_vs_frequency = plt.subplot(111)
    cax = plt.specgram(pressure * 100000, NFFT = 256, Fs = 90000, cmap=plt.cm.gist_heat, zorder = 1)
    spectorgram_pressure_vs_frequency.grid(False, which="major")
    spectorgram_pressure_vs_frequency.set_xlabel('Time (s)', labelpad=6)
    spectorgram_pressure_vs_frequency.set_ylabel('Frequency (Hz)', labelpad=6)
    y_min, y_max = spectorgram_pressure_vs_frequency.get_ylim()
    # plt.gca
    cbar = plt.colorbar(orientation='vertical', ax = spectorgram_pressure_vs_frequency, fraction = 0.046, pad = 0.2)
    cbar.set_label('Power spectral density (dB)', rotation=90)
    primary_ticks = len(spectorgram_pressure_vs_frequency.yaxis.get_major_ticks())
    pressure_vs_time = spectorgram_pressure_vs_frequency.twinx()
    pressure_vs_time.plot(((theta + 360) / (6 * 6600)), pressure, linewidth = 0.75, linestyle = '-', color = '#FFFFFF', zorder = 2)
    pressure_vs_time.grid(b = True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)
    spectorgram_pressure_vs_frequency.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)
    pressure_vs_time.set_ylabel('Cylinder pressure (bar)', labelpad=6)
    pressure_vs_time.yaxis.set_major_locator(mtick.LinearLocator(primary_ticks))
    spectorgram_pressure_vs_frequency.set_xlim([0, max(cax[2])])
    spectorgram_pressure.savefig(bbox_inches='tight')
    plt.close()

如何像 Python 中的 y 轴网格一样将 x 轴网格放置在频谱图的顶部? 我使用的是 matplotlib 版本 1.3.1。 这是特定于版本的问题吗?

更新

我将 matplotlib 从版本 1.3.1 更新到 1.4.3,即使这样我也无法设置 x 轴网格。

【问题讨论】:

  • 如果我运行你的代码,我会在 x 和 y 方向上得到网格线。您的matplotlibrc 中是否设置了可能会影响事物的内容?也许尝试使用 matplotlib.rcdefaults() 将它们设置为默认值
  • 即使在我的 matplotlibrc 网格线中。 grid.color : FFFFFF grid.linestyle : - grid.linewidth : 0.5 grid.alpha : 1.0
  • 什么版本的matplotlib?可能是版本问题。
  • 我的彩条周围还有一个黑框,与您的图像不同。这些细微的差异让我想知道是什么引擎在渲染您的 pdf。你能给我们更多的设置细节吗?您是否使用usetex 选项 - 如果是,您使用的是什么操作系统以及渲染 TeX 的程序是什么?如果您渲染到屏幕上,您是否看到垂直线(例如plt.show())?如果我降低线宽,我可以在屏幕上使用plt.show() 遇到与您类似的问题。您是否尝试过在对grid()xaxis.grid() 的调用中增加线宽?试试 1.5 ish。
  • OK - 我将 Ubuntu 14.04 64 位新安装到虚拟机,新安装软件包 python-matplotlib 并且如果“一切正常”。我在屏幕和 pdf 中看到垂直 x 轴网格线。以防万一,我尝试设置text.usetex=True 和渲染(必须安装livetex-latex-baselivetex-fonts-recommendedlivetex-fonts-extralivetex-latex-extra)。这也适用于屏幕和呈现为 pdf。我无法重现您的错误。你的系统还有什么不寻常的地方吗?这段代码的 sn-p 是否是您使用 mpl 所做的更大的事情的一部分?

标签: python matplotlib


【解决方案1】:

虽然在我的情况下(python3.4、matplotlib 1.4.2、Ubuntu 14)我看到了 x-gridlines,但我想你可以尝试以下操作:

[pressure_vs_time.xaxis.get_gridlines()[x].zorder for x in range(len(pressure_vs_time.xaxis.get_gridlines())) ]

这将为您提供网格线的图层(上部具有较高的值,也可以是负数)。在我的情况下,它为所有行返回 2。比你可以设置:

[pressure_vs_time.xaxis.get_gridlines()[x].zorder = 1000 for x in range(len(pressure_vs_time.xaxis.get_gridlines())) ]

【讨论】:

  • y 轴网格线对我来说没问题。 X 轴网格线是问题所在。
  • 查看更新。我都看到了。我刚刚将yaxis 更改为xaxis。试试看。
【解决方案2】:

我已经复制了您问题中提供的代码,它对我来说很好用。我看到了 x 和 y 网格线。您的代码会自动保存图像的 pdf 版本。我将此输出转换为 png。看起来是这样的

我正在使用 Ubuntu 14.4 LTS、python 2.7.6、numpy 1.8.2 和 matplotlib 1.3.1。

【讨论】:

    【解决方案3】:

    正如其他人所指出的 - 使用您提供的代码来复制您的问题非常困难。

    特别是 - 我已经在 Windows 8.1、Ubuntu 14.04(在 Virtualbox VM 上)、matplotlib 版本 1.3.1 和 1.4.3、设置和不设置 text.usetex 以及使用 Python 2.7.6 和 Python 3 进行了尝试。没有他们使用您提供的代码重现您的问题。

    然而

    可以复制你看到的内容,如果我替换该行

    spectorgram_pressure_vs_frequency.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)
    

    pressure_vs_time.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)
    

    即我尝试将xaxis.grid 设置在双轴而不是原始轴上。我必须得出结论,在您的真实代码中,您以某种方式将xaxis.grid 设置在双轴而不是主轴上。


    这意味着我可以如下回答您的直接问题:

    如何像 Python 中的 y 轴网格一样将 x 轴网格放置在频谱图的顶部?

    您的代码执行此操作 - 您在原始(非孪生)轴上调用 xaxis.grid()

    我使用的是 matplotlib 1.3.1 版。这是特定于版本的问题吗?

    不,1.3.1 和 1.4.3 中的行为相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2023-02-14
      相关资源
      最近更新 更多