3

I want to use the code from Polar histogram in Python for given r, theta and z values, replaced with my dataframe columns df.azimuth, df.zenith, and df.ozone (average for each bin).

I'm having issues converting dataframe .values format, to a polar plot format.

I would like to produce a polar plot with my DataFrame columns df.azimuth, df.zenith, and df.o3 average for each bin as showed on the plot example that I attached here.

I'm having issues converting DataFrame format, to a polar plot format. I don't know how to do this from a DataFrame format. Any help is welcome.

That is my code so far, but it is not showing the plot correctly.

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

Desired Output Shape: image output from this code

Traceback

---------------------------------------------------------------------------
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')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

2
  • Do not change the scope of the question, after it has been answered.
  • There seems to be two main issues
    1. x, y, and z are the incorrect columns.
      • .iloc[:,0] is the time column
      • .iloc[:,1] is the zenith column
      • .iloc[:,2] is the aximuth column
      • UFuncTypeError occurs because radius is the 'time' column of the dataframe.
    2. azimuth should be in radians, according to the cited sample code.
  • zenith needs to be in radians for the density function to return the expected averges.
  • o3 is assumed to be the concentration and will be used as the weight
    • The concentration of the o3 column is normalized by using density=True inside np.histogram2d().
  • There is no reason to extract the values of each dataframe column, with .values. The operations will accept a dataframe column.
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

Polar plot with the average

  • Histograms return the number of values in the bins
  • The weighted histogram is the sum of all the weights for each given bin
  • Divide the two, to get the average.
    • This will have a RuntimeWarning: invalid value encountered in true_divide because there will be 0/0. Those instances are returned as 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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158