python - 如何创建具有方位角、天顶角和平均权重值的极坐标图?

标签 python pandas numpy matplotlib plot

我想使用 Polar histogram in Python for given r, theta and z values 中的代码,替换为我的数据框列 df.azimuthdf.zenithdf.ozone(每个 bin 的平均值)。

我在将数据帧 .values 格式转换为极坐标图格式时遇到问题。

我想用我的 DataFrame 列 df.azimuthdf.zenithdf.o3 平均值生成一个极坐标图每个箱子都在我附在此处的绘图示例中显示。

我在将 DataFrame 格式转换为极坐标图格式时遇到问题。我不知道如何从 DataFrame 格式执行此操作。欢迎任何帮助。

到目前为止,这是我的代码,但它没有正确显示情节。

数据框(o3Pan_wff):

time,zenith,azimuth,o3
2017-07-03 16:48:27-04:00,49.47,269.05,293.51
2017-07-03 16:50:05-04:00,49.79,269.31,296.54
2017-07-03 16:51:58-04:00,50.16,269.6,322.78
2017-07-03 16:53:58-04:00,50.56,269.91,308.76
2017-07-03 16:55:49-04:00,50.89,270.16,292.61
2017-07-03 17:02:04-04:00,52.15,271.13,289.28
2017-07-03 17:03:37-04:00,52.46,271.37,287.94
2017-07-03 17:05:10-04:00,52.76,271.6,289.24
2017-07-03 17:06:43-04:00,53.07,271.83,289.29
2017-07-03 17:08:17-04:00,53.38,272.06,290.1
2017-07-03 17:09:51-04:00,53.69,272.3,289.98
2017-07-03 17:11:24-04:00,53.99,272.52,290.3
2017-07-03 17:12:59-04:00,54.28,272.74,292.68
2017-07-03 17:14:33-04:00,54.62,272.98,290.96
2017-07-03 17:16:13-04:00,54.95,273.23,286.32
2017-07-03 17:18:03-04:00,55.31,273.49,291.36
2017-07-03 17:19:42-04:00,55.6,273.71,286.33
2017-07-03 17:21:16-04:00,55.9,273.93,286.73
2017-07-03 17:22:49-04:00,56.24,274.18,288.12
2017-07-03 17:24:22-04:00,56.55,274.4,289.78
2017-07-03 17:25:55-04:00,56.85,274.63,290.34
2017-07-03 17:27:27-04:00,57.15,274.84,287.5
2017-07-03 17:29:00-04:00,57.46,275.06,287.56
2017-07-03 17:32:16-04:00,58.1,275.53,287.42
2017-07-03 17:33:49-04:00,58.41,275.75,288.22
2017-07-03 17:35:22-04:00,58.71,275.97,288.21
2017-07-03 17:36:54-04:00,59.01,276.18,289.33
2017-07-03 17:38:27-04:00,59.32,276.4,288.19
2017-07-03 17:40:00-04:00,59.62,276.62,288.63
2017-07-03 17:41:33-04:00,59.92,276.83,287.48
2017-07-03 17:43:05-04:00,60.22,277.05,289.64
2017-07-03 17:44:38-04:00,60.49,277.24,290.85
2017-07-03 17:46:11-04:00,60.83,277.48,289.8
2017-07-03 17:47:44-04:00,61.13,277.69,289.04
2017-07-03 17:49:17-04:00,61.44,277.91,288.4
2017-07-03 17:50:50-04:00,61.74,278.12,287.36
2017-07-03 17:52:23-04:00,62.04,278.34,288.56
2017-07-03 17:53:56-04:00,62.35,278.55,290.08
2017-07-03 17:55:28-04:00,62.65,278.76,289.27
2017-07-03 17:57:02-04:00,62.95,278.98,290.48
2017-07-03 17:58:34-04:00,63.25,279.19,291.09
2017-07-03 18:01:56-04:00,63.91,279.65,290.26
2017-07-03 18:03:29-04:00,64.17,279.83,290.04
2017-07-03 18:05:03-04:00,64.51,280.08,290.78
2017-07-03 18:06:36-04:00,64.82,280.29,290.92
2017-07-03 18:08:09-04:00,65.12,280.5,290.22
2017-07-03 18:09:43-04:00,65.42,280.72,290.59
2017-07-03 18:11:18-04:00,65.69,280.91,291.26
2017-07-03 18:12:52-04:00,66.03,281.15,290.02
2017-07-03 18:14:26-04:00,66.34,281.36,290.87
2017-07-03 18:15:59-04:00,66.62,281.57,290.38
2017-07-03 18:17:32-04:00,66.89,281.75,288.86
2017-07-03 18:19:05-04:00,67.23,282.0,288.56
2017-07-03 18:20:38-04:00,67.53,282.21,289.96
2017-07-03 18:22:12-04:00,67.83,282.42,288.42
2017-07-03 18:23:45-04:00,68.13,282.63,289.89
2017-07-03 18:25:18-04:00,68.43,282.84,287.91
2017-07-03 18:26:51-04:00,68.73,283.05,287.83
2017-07-03 18:28:24-04:00,69.03,283.26,288.11
2017-07-03 18:31:40-04:00,69.65,283.71,288.18
2017-07-03 18:33:13-04:00,69.95,283.92,287.93
2017-07-03 18:34:46-04:00,70.25,284.13,288.6
2017-07-03 18:36:19-04:00,70.55,284.34,287.21
2017-07-03 18:37:51-04:00,70.84,284.55,287.95
2017-07-03 18:39:24-04:00,71.13,284.76,288.01
2017-07-03 18:40:57-04:00,71.43,284.97,287.84
2017-07-03 18:42:30-04:00,71.73,285.18,287.76
2017-07-03 18:44:03-04:00,72.02,285.39,288.32
2017-07-03 18:45:36-04:00,72.32,285.6,288.63
2017-07-03 18:47:09-04:00,72.61,285.82,287.33
2017-07-03 18:48:42-04:00,72.9,286.03,288.74
2017-07-03 18:50:16-04:00,73.18,286.22,289.99
2017-07-03 18:51:49-04:00,73.47,286.44,289.13
2017-07-03 18:53:23-04:00,73.79,286.67,286.91
2017-07-03 18:54:59-04:00,74.1,286.89,285.5
2017-07-03 18:56:34-04:00,74.39,287.1,289.98
2017-07-03 18:58:09-04:00,74.66,287.3,285.98
2017-07-03 19:01:37-04:00,75.32,287.78,290.05
2017-07-03 19:03:11-04:00,75.64,288.01,286.12
2017-07-03 19:04:46-04:00,75.94,288.23,288.4
2017-07-03 19:06:19-04:00,76.23,288.45,285.19
2017-07-03 19:23:43-04:00,79.44,290.85,283.02
2017-07-03 19:25:19-04:00,79.76,291.09,282.24
2017-07-03 19:26:53-04:00,80.05,291.31,281.31
2017-07-03 19:28:28-04:00,80.34,291.53,278.59
2017-07-03 19:30:07-04:00,80.64,291.77,276.28
2017-07-04 06:51:25-04:00,78.99,69.55,276.16
2017-07-04 06:53:11-04:00,78.66,69.79,290.81
2017-07-04 06:54:51-04:00,78.36,70.03,285.92
2017-07-04 08:01:01-04:00,65.81,79.07,292.54
2017-07-04 08:02:37-04:00,65.47,79.31,291.46
2017-07-04 08:04:16-04:00,65.15,79.54,289.5
2017-07-04 08:06:02-04:00,64.82,79.77,293.57
2017-07-04 08:07:44-04:00,64.48,80.01,291.61
2017-07-04 08:09:24-04:00,64.18,80.22,289.69
2017-07-04 08:12:40-04:00,63.52,80.69,292.57
2017-07-04 08:14:15-04:00,63.21,80.91,292.02
2017-07-04 08:15:50-04:00,62.9,81.13,290.69
2017-07-04 08:17:26-04:00,62.59,81.35,292.06
2017-07-04 08:19:01-04:00,62.28,81.57,291.52
import matplotlib.pyplot as plt
import pandas as pd

o3Pan_wff = pd.read_csv('test.csv')

x=o3Pan_wff.iloc[:,0].values # zenith
y=o3Pan_wff.iloc[:,1].values # azimuth
z=o3Pan_wff.iloc[:,2].values # ozone

# two input arrays
azimut = o3Pan_wff.iloc[:,1].values
radius = o3Pan_wff.iloc[:,0].values

# define binning
rbins = np.linspace(0,radius.max(), 30)
abins = np.linspace(0,2*np.pi, 60)

A, R = np.meshgrid(abins, rbins)

# plot
fig, ax = plt.subplots(subplot_kw=dict(projection="polar"))

pc = ax.pcolormesh(A, R, z, cmap="magma_r")
fig.colorbar(pc)

plt.show()

所需的输出形状: image output from this code

回溯

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-156-d6575f1fd82c> in <module>
     10 
     11 # define binning
---> 12 rbins = np.linspace(0,radius.max(), 30)
     13 abins = np.linspace(0,2*np.pi, 60)
     14 

<__array_function__ internals> in linspace(*args, **kwargs)

e:\Anaconda3\lib\site-packages\numpy\core\function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
    119     # and make sure one can use variables that have an __array_interface__, gh-6634
    120     start = asanyarray(start) * 1.0
--> 121     stop  = asanyarray(stop)  * 1.0
    122 
    123     dt = result_type(start, stop, float(num))

UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

最佳答案

  • 在回答问题后不要改变问题的范围。
  • 似乎有两个主要问题
    1. xyz 是不正确的列。
      • .iloc[:,0]时间
      • .iloc[:,1]zenith
      • .iloc[:,2]aximuth
      • UFuncTypeError 发生是因为 radius 是数据帧的 'time' 列。
    2. azimuth 根据引用的示例代码,应以弧度为单位。
  • zenith 需要以弧度为单位,density 函数才能返回预期的平均值。
  • o3 被假定为浓度并将用作权重
    • o3 列的浓度通过在 np.histogram2d() 中使用 density=True 进行归一化。
  • 没有理由使用 .values 提取每个数据框列的值。这些操作将接受数据框列。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# read data
df = pd.read_csv('test.csv')

# set time as datatime
df.time = pd.to_datetime(df.time, utc=True)

# convert azimuth and zenith to radians
df[['azimuth', 'zenith']] = df[['azimuth', 'zenith']].apply(np.radians)

|    | time                      |   zenith |   azimuth |     o3 |
|---:|:--------------------------|---------:|----------:|-------:|
|  0 | 2017-07-03 20:48:27+00:00 | 0.863414 |   4.69581 | 293.51 |
|  1 | 2017-07-03 20:50:05+00:00 | 0.868999 |   4.70035 | 296.54 |
|  2 | 2017-07-03 20:51:58+00:00 | 0.875457 |   4.70541 | 322.78 |

# define binning
rbins = np.linspace(0, df.zenith.max(), 30)
abins = np.linspace(0, 2*np.pi, 60)

# calculate histogram
hist, _, _ = np.histogram2d(df.azimuth, df.zenith, density=True, weights=df.o3, bins=(abins, rbins))
A, R = np.meshgrid(abins, rbins)

# plot
fig, ax = plt.subplots(subplot_kw=dict(projection="polar"), figsize=(7, 7))

pc = ax.pcolormesh(A, R, hist.T, cmap="magma_r")
fig.colorbar(pc)
plt.grid()
plt.show()

enter image description here

带平均值的极坐标图

  • 直方图返回 bin 中值的数量
  • 加权直方图是每个给定 bin 的所有权重的总和
  • 将两者相除,得到平均值。
    • 这将有一个 RuntimeWarning: invalid value encountered in true_divide 因为会有 0/0。这些实例返回为 NaN
hist, _, _ = np.histogram2d(df.azimuth, df.zenith, bins=(abins, rbins), weights=df.o3)
hist2, _, _ = np.histogram2d(df.azimuth, df.zenith, bins=(abins, rbins))

avg_hist = hist / hist2

A, R = np.meshgrid(abins, rbins)

# plot
fig, ax = plt.subplots(subplot_kw=dict(projection="polar"), figsize=(7, 7))

enter image description here

关于python - 如何创建具有方位角、天顶角和平均权重值的极坐标图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63758336/

相关文章:

python - 使用 SciPy 二维插值器时出错

python - 迭代 dask 系列(从 dask 系列获取唯一值以列表)

python - 有没有办法让 Seaborn 或 Vincent 互动?

python - Numpy、掩蔽和 sklearn 聚类

python - 检测 → Python 中的符号

Python格式函数: 1 decimal place

python - ValueError : only single character unicode strings can be converted to Py_UCS4, 长度为 0

python - 将 Int64Index 更改为 Index 并将 dtype=int64 更改为 dtype=object

python - 如何在PyTorch中正确使用Numpy的FFT函数?

python - 在用 python 生成的图形中添加一些数字