如何设置直方图的轴并互换x轴和y轴?

3 投票
1 回答
31822 浏览
提问于 2025-04-21 01:47

我有一个数据集,想要绘制一个直方图,显示每个深度的测量次数。我想把深度这一列的数据分成每5米一组,从0米开始,一直到155米。这个代码可以生成一个直方图,看起来形状还不错,但数值似乎不太对,而且我无法让它从0开始。此外,我还希望能够交换x轴和y轴,让深度在y轴上,测量频率在x轴上。

    import numpy as np
    import datetime as dt
    import matplotlib.pyplot as plt
    import glob


    #The data is read in, and then formatted so that an array Dout (based on depth) is created. This is done since multiple files will be read into the code, and so I can create the histogram for all the files I have up until this point.

    Dout = np.array(depthout)


    bins = np.linspace(0, 130, num=13) #, endpoint=True, retstep=False)


    plt.hist(Dout, bins)
    plt.xlabel('Depth / m')
    plt.ylabel('Frequency')
    plt.show()


    # the end

数据的格式是这样的:

TagID    ProfileNo   ProfileDirn     DateTime    Lat     Lon     Depth   Temperature

1 个回答

8

你想要使用 hist 函数中的 orientation 这个参数(文档链接)。

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

data = np.random.randn(1500)
bins = np.linspace(-5, 5, 25, endpoint=True)

ax.hist(data, bins, orientation='horizontal')
ax.set_ylim([-5, 5])
plt.show()

在这里输入图片描述

撰写回答