简体   繁体   English

如何在直方图上设置轴并交换x和y轴?

[英]How do you set the axes on a histogram and swap the x and y axes?

I have a dataset where I want to plot a histogram of the number of measurements at each depth. 我有一个数据集,我想绘制每个深度的测量数量的直方图。 I want to plot a histogram of each the frequency of measurements in the depth column. 我想绘制深度列中每个测量频率的直方图。 I want to bin the data into 5m chuncks starting at 0m, going up to 155m. 我希望将数据从0m开始分成5m的块,最高可达155m。 This code, gets me a histogram, which looks a reasonable shape, but the values seem wrong, and I can't quite get it to start at 0. In addition, I would like to be able to swap the x-axis and y-axis, so that depth is on the y-axis, and frequency, is on the x-axis. 这个代码,给我一个直方图,它看起来是一个合理的形状,但值似乎是错误的,我不能让它从0开始。另外,我希望能够交换x轴和y -axis,使深度在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

The data is in this format: 数据采用以下格式:

TagID    ProfileNo   ProfileDirn     DateTime    Lat     Lon     Depth   Temperature

You want the orientation kwarg of hist ( Docs ). 你想要histDocs )的orientation kwarg。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM