如何添加第二个(圆形)直方图从csv文件中读取?

时间:2018-08-22 04:25:32

标签: python csv matplotlib histogram

我是python的新手,已经整理了一些代码,这些代码从.csv文件的第一个索引中读取(包含以弧度表示的值),并绘制了直方图和圆形直方图。我想添加第二个(圆形)直方图,它从第二个索引读取。谁能告诉我该怎么做?

完整代码:

import numpy as np
import matplotlib.pyplot as plt
import csv

with open('radians.csv', 'r') as f:
        reader=csv.reader(f)
        angles=[] # Initialise empty list
        next(reader) # Skip header line
        for row in reader:
                angle = float(row[0]) 
                angles.append(angle)

bins_number = 18  # the [-180, 180) interval will be subdivided into this
bins = np.linspace(-np.pi, np.pi, bins_number + 1)

n, _, _ = plt.hist(angles, bins) # Create histogram
plt.show()

# Create circular histogram

plt.clf()
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 1, 1, projection='polar')
bars = ax.bar(bins[:bins_number], n, width=width, bottom=10.0, align='edge', color='red')
for bar in bars:
    bar.set_alpha(0.5)
plt.show()

rostral.csv值示例:

1.214109733,2.678066227
1.214109733,2.378408071
1.214109733,2.378408071
1.290159115,2.314906
1.193219453,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
-1.7325846,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.208196994,2.314906
1.248951138,2.314906
0.945157766,2.314906
-1.343997479,2.314906
-1.561624822,2.314906
-1.903895159,2.314906

1 个答案:

答案 0 :(得分:0)

您可以在阅读第一列的同时创建第二组角度。然后可以使用子图显示所有数据,以便为所有图形提供2 x 2的布局:

import numpy as np
import matplotlib.pyplot as plt
import csv

angles1 = []
angles2 = []

with open('radians.csv', 'r') as f:
    reader = csv.reader(f)
    next(reader) # Skip header line

    for row in reader:
        angles1.append(float(row[0]))
        angles2.append(float(row[1]))


bins_number = 18  # the [-180, 180) interval will be subdivided into this
bins = np.linspace(-np.pi, np.pi, bins_number + 1)

ax = plt.subplot(2, 2, 2)
n1, _, _ = plt.hist(angles1, bins) # Create histogram
ax = plt.subplot(2, 2, 4)
n2, _, _ = plt.hist(angles2, bins) # Create histogram

# Create circular histograms
width = 2 * np.pi / bins_number
ax = plt.subplot(2, 2, 1, projection='polar')
bars1 = ax.bar(bins[:bins_number], n1, width=width, bottom=10.0, align='edge', color='red', alpha=0.5)
ax = plt.subplot(2, 2, 3, projection='polar')
bars2 = ax.bar(bins[:bins_number], n2, width=width, bottom=10.0, align='edge', color='red', alpha=0.5)

plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()

这将为您提供一个图形,如下所示:

matplot figure


这些图可以组合如下:

import numpy as np
import matplotlib.pyplot as plt
import csv

angles1 = []
angles2 = []

with open('radians.csv', 'r') as f:
    reader = csv.reader(f)
    next(reader) # Skip header line

    for row in reader:
        angles1.append(float(row[0]))
        angles2.append(float(row[1]))


bins_number = 18  # the [-180, 180) interval will be subdivided into this
bins = np.linspace(-np.pi, np.pi, bins_number + 1)

ax = plt.subplot(1, 2, 2)
n1, _, _ = plt.hist(angles1, bins, color='red') # Create histogram
n2, _, _ = plt.hist(angles2, bins, color='blue') # Create histogram

# Create circular histograms
width = 2 * np.pi / bins_number
ax = plt.subplot(1, 2, 1, projection='polar')
bars1 = ax.bar(bins[:bins_number], n1, width=width, bottom=10.0, align='edge', color='red', alpha=0.5)
bars2 = ax.bar(bins[:bins_number], n2, width=width, bottom=10.0, align='edge', color='blue', alpha=0.5)

plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()

将合并后的输出提供为:

matplot output - combined graphs