如何从这个nc文件制作直方图? [英] How to make a histogram from this nc file?

查看:236
本文介绍了如何从这个nc文件制作直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名研究助理,最近我开始学习python来解释netCDF文件格式的模型输出。让我给出一个关于我的问题的快速背景:



我已经使用netCDF4模块搜索了netCDF文件的某个网格区域,并存储了一系列的时间,然后,我使用netCDF4的num2date功能转换为日期列表。我在下面显示了我的代码请注意,restrictedrange是nc文件中变量的一部分,rmduplicates()未显示。

 将netCDF4导入为nc 
import numpy as np
import matplotlib.pyplot as pyp
import matplotlib as mpl
import datetime as dtm
flor = nc.Dataset('FLOR.slp_subset1.nc ','r')

times = []
timecounter = .25
for i in limitedrange:
for j in np.nditer(i):
if(j< = 975):
times.append(timecounter)
timecounter + = .25
uniquetimes = rmduplicates(times)
dates = nc.num2date (uniquetimes0001-01-01 00:00:00,'julian')

stacked_dates = []
日期中的日期:
stacked_dates.append (date.replace(year = 0001))
stacking_dates = mpl.dates.date2num(stacking_dates)

fig = pyp.figure()
ax = pyp.subplot(111 )
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
format = mpl.dates.DateFormatter('%m / %d')
ax.xaxis.set_major_formatter(格式)

ax.hist(stacked_dates)

pyp.xticks(rotation ='vertical')

pyp.show()

现在我有一个格式的日期列表(y)yy-mm-dd hh:mm:ss。我现在想要采取这些日期,并制作一个直方图(可能使用matplotlib或任何最适合这个)在一个月。所以,酒吧=频率,箱子是几个月。
另外,如果我的格式不清楚,有些年份有三个数字,只有两个,但实际上没有一个。



我非常喜欢python,所以我感谢任何帮助,如果这个问题格式不好,我从来没用过这个网站,我很抱歉。



谢谢! >

解决方案

我不知道你有什么数据,但这是一个模拟示例,如何制作一个直方图,几个月。 x轴。



我只能假设你从一个datetime对象列表开始,但是我无法弄清楚什么是 nc 是(是matplotlib.date模块吗?)或什么样的时间可以在唯一的时间内找到。所以通常这是方法。



您将需要使用的这些模块。

  import matplotlib as mpl 
import matplotlib.pyplot as plt
import datetime

这些都是模拟我使用的日期对于这个例子在那里只有11个月,所以大多数所有的垃圾桶都将是1。到目前为止,我的大部分都是1。

  12):
dates.append(datetime.datetime(i * 5 + 1960,i,i,i,i,i))

[datetime.datetime(1965,1,1 ,1,1,1),datetime.datetime(1970,2,2,2,2,2),datetime.datetime(1975,3,3,3,3),datetime.datetime(1980,4, 4,4,4,4),datetime.datetime(1985,5,5,5,5,5),datetime.datetime(1990,6,6,6,6,6),datetime.datetime(1995,7 ,7,7,7,7),datetime.datetime(2000,8,8,8,8,8),datetime.datetime(2005,9,9,9,9),datetime.datetime(2010, 10,10,10,10,10),datetime.datetime(2015,11,11,11,11,11)]

如果像上面的例子一样,你正在处理不同的年份,你将不得不自己堆叠他们。否则,稍后使用的 date2num 函数会产生大量不同的数字。为了堆叠,他们意味着转换它们,就像在同一年中发生的一样。

  stacked_dates = [] 
日期在日期:
stacked_dates.append(date.replace(year = 2000))

>>> stacking_dates
[datetime.datetime(2000,1,1,1,1,1),datetime.datetime(2000,2,2,2,2,2),datetime.datetime(2000,3,3, 3,3,3),datetime.datetime(2000,4,4,4,4,4),datetime.datetime(2000,5,5,5,5,5),datetime.datetime(2000,6,6 ,6,6,6),datetime.datetime(2000,7,7,7,7,7),datetime.datetime(2000,8,8,8,8,8),datetime.datetime(2000,9, 9,9,9,9),datetime.datetime(2000,10,10,10,10,10),datetime.datetime(2000,11,11,11,11,11)]

好的。现在我们可以使用 date2num 函数来获取某些 mpl 实际上了解的东西。 (Btw,如果你想绘制这个数据,你可以用 plt.plot_dates 函数,该函数可以理解datetime对象)

  stacked_dates = mpl.dates.date2num(stacked_dates)

>>> stacked_dates
数组([730120.04237269,730152.08474537,730182.12711806,
730214.16949074,730245.21186343,730277.25423611,
730308.2966088,730340.33898148,730372.38135417,
730403.42372685,730435.46609954])

现在确定自己的绘图。 mpl 可以理解这些数字,但是不会自动假定它们是日期。它会将它们视为正常数字。这就是为什么我们必须告诉x轴他们实际上是日期。使用 major_axis_formatter set_major_locator

  fig = plt.figure()
ax = plt.subplot(111)
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
format = mpl .date.DateFormatter('%m /%d')#explore其他选项显示
ax.xaxis.set_major_formatter(格式)

ax.hist(stacked_dates)#plot the damned thing

plt.xticks(rotation ='vertical')#avoid重复的数字
#make确保你这样做后面的.hist函数

plt.show()

此代码生成以下图表:



< img src =https://i.stack.imgur.com/oRq8Y.pngalt =enter image description here>



请注意,有机会您将无法在原始图形上看到日期,因为它们将会从屏幕上运行(像这样的格式可能很长,并且不适合图形)。在这种情况下,按配置子图按钮并调整底部的值。在脚本中,您可以通过 plt.subplots_adjust(bottom = .3)或其他一些值来执行此操作。



您还应该注意在 ax.hist(stacked_dates,bins = 12)中有12个bin。

还有一个更简单,虽然不太可修改/人物可爱等可能性通过使用条形图,而不是直方图。了解它 HERE 但它确实取决于你有什么样的信息。如果这是很多日期,那么让$ code> hist 函数计算bin高度可能比自己更容易。
如果是其他一些信息,值得考虑使用条形图。



完整的脚本将是这样的:

  import matplotlib as mpl 
import matplotlib.pyplot as plt
import datetime

stacked_dates = []
在日期的日期:
stacked_dates.append(date.replace(year = 2000))

stacking_dates = mpl.dates.date2num(stacking_dates)

fig = plt.figure()
ax = plt.subplot(111)
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
格式= mpl.dates.DateFormatter('%m /%d')
ax.xaxis.set_major_formatter(format)

ax.hist(stacked_dates)

plt.xticks(rotation ='vertical')
plt.show()


I'm a research assistant and I've recently started to learn python to interpret model output in netCDF file format. Let me give a quick background on my question:

I have already searched through a certain grid area of a netCDF file using the netCDF4 module and stored an array of times, which I then converted to a list of dates using netCDF4's num2date feature. I have shown my code below. Please note that restrictedrange is a subset of a variable from an nc file and rmduplicates() is not shown.

import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as pyp
import matplotlib as mpl
import datetime as dtm
flor = nc.Dataset('FLOR.slp_subset1.nc','r')    

times = []
timecounter = .25
for i in restrictedrange:
     for j in np.nditer(i):
         if(j <= 975):
              times.append(timecounter)
    timecounter += .25
uniquetimes = rmduplicates(times)
dates = nc.num2date(uniquetimes,'days since 0001-01-01 00:00:00','julian')

stacked_dates = []
for date in dates:
    stacked_dates.append(date.replace(year=0001))
stacked_dates = mpl.dates.date2num(stacked_dates)

fig = pyp.figure()
ax = pyp.subplot(111)
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
format = mpl.dates.DateFormatter('%m/%d')
ax.xaxis.set_major_formatter(format)

ax.hist(stacked_dates)

pyp.xticks(rotation='vertical')

pyp.show()

Now I have a list of dates in the format "(y)yy-mm-dd hh:mm:ss". I would now like to take those dates and make a histogram (possibly using matplotlib or whatever is best for this) by month. So, bars = frequency, bins are months. Also, if it wasn't clear from my format, some years have three numbers, some only two, but actually none that have 1.

Again, I'm quite new to python so I appreciate any help and I apologize if this question is poorly formatted, as I have never used this website.

Thanks!

解决方案

I don't know what you have for data, but here's an mock example of how to make a histogram with months\days on x axis.

I can only assume that you start with a list of datetime objects, but I can't figure out what nc is (is that matplotlib.date module?) or what kind of times can exactly be found in the unique times. So generally this is the approach.

These modules you will need and use.

import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime

These are the mock dates I've used. for this example. There are only 11 months on there, so mostly all bins will be 1 in the end.

for i in range(1, 12):
    dates.append(datetime.datetime(i*5+1960, i, i, i, i, i))

[datetime.datetime(1965, 1, 1, 1, 1, 1), datetime.datetime(1970, 2, 2, 2, 2, 2), datetime.datetime(1975, 3, 3, 3, 3, 3), datetime.datetime(1980, 4, 4, 4, 4, 4), datetime.datetime(1985, 5, 5, 5, 5, 5), datetime.datetime(1990, 6, 6, 6, 6, 6), datetime.datetime(1995, 7, 7, 7, 7, 7), datetime.datetime(2000, 8, 8, 8, 8, 8), datetime.datetime(2005, 9, 9, 9, 9, 9), datetime.datetime(2010, 10, 10, 10, 10, 10), datetime.datetime(2015, 11, 11, 11, 11, 11)]

If like in the above example you're dealing with different years, you're going to have to "stack" them yourself. Otherwise the date2num function I'll use later will produce wildly different numbers. To "stack" them means convert them as if they all happened in the same year.

stacked_dates = []
for date in dates:
    stacked_dates.append( date.replace(year=2000)  )

>>> stacked_dates
[datetime.datetime(2000, 1, 1, 1, 1, 1), datetime.datetime(2000, 2, 2, 2, 2, 2), datetime.datetime(2000, 3, 3, 3, 3, 3), datetime.datetime(2000, 4, 4, 4, 4, 4), datetime.datetime(2000, 5, 5, 5, 5, 5), datetime.datetime(2000, 6, 6, 6, 6, 6), datetime.datetime(2000, 7, 7, 7, 7, 7), datetime.datetime(2000, 8, 8, 8, 8, 8), datetime.datetime(2000, 9, 9, 9, 9, 9), datetime.datetime(2000, 10, 10, 10, 10, 10), datetime.datetime(2000, 11, 11, 11, 11, 11)]

Ok. Now we can use the date2num function to get something mpl actually understands. (Btw, if you want to plot just this data you can with plt.plot_dates function, that function understands datetime objects)

stacked_dates = mpl.dates.date2num(stacked_dates)

>>> stacked_dates
array([ 730120.04237269,  730152.08474537,  730182.12711806,
        730214.16949074,  730245.21186343,  730277.25423611,
        730308.2966088 ,  730340.33898148,  730372.38135417,
        730403.42372685,  730435.46609954])

Ok now for the plotting itself. mpl can understand these numbers, but it will not automatically assume they are dates. It will treat them as normal numbers. That's why we've got to tell the x axis that they're actually dates. Do that with major_axis_formatter and set_major_locator

fig = plt.figure()
ax = plt.subplot(111)
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
format = mpl.dates.DateFormatter('%m/%d') #explore other options of display
ax.xaxis.set_major_formatter(format)

ax.hist(stacked_dates) #plot the damned thing

plt.xticks(rotation='vertical') #avoid overlapping numbers
                           #make sure you do this AFTER .hist function

plt.show()

This code produces following graph:

Do note that there's a chance you won't be able to see dates on your original graph because they'll run off screen (formats like these can be long, and don't fit on the graph). In that case press the "configure subplots" button and adjust value for "bottom". In the script you can do that by plt.subplots_adjust(bottom=.3) or some other value.

You should also take care to specify that there are 12 bins in ax.hist(stacked_dates, bins=12) because default is 10, and will look funky like my graph.

Also there's a simpler, albeit less modifiable/personofiable etc... possibility by using a bar plot, instead of a histogram. Read about it HERE But it really depends on what kind of information you have. If it's a lot of dates, it's probably easier to let the hist function calculate bin heights than doing it by yourself. If it's some other info, it's worthwhile to consider using a bar plot.

Complete script would be something like:

import matplotlib as mpl
import matplotlib.pyplot as plt
import datetime

stacked_dates = []
for date in dates:
    stacked_dates.append( date.replace(year=2000)  )

stacked_dates = mpl.dates.date2num(stacked_dates)

fig = plt.figure()
ax = plt.subplot(111)
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
format = mpl.dates.DateFormatter('%m/%d')
ax.xaxis.set_major_formatter(format)

ax.hist(stacked_dates)

plt.xticks(rotation='vertical')  
plt.show()

这篇关于如何从这个nc文件制作直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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