Last active 1 year ago

README.md Raw
distribution_plot.py Raw
1import os
2import pandas as pd
3import numpy as np
4import matplotlib.pyplot as plt
5
6# Open the file "./hipparcos-voidmain.csv" for reading
7f = open("./hipparcos-voidmain.csv", "r")
8
9dataset = pd.read_csv(f, sep=",", header=0)
10
11# Get the data we want to plot
12# Apparent magnitude in the V band (visible light)
13apparentMag = np.array(dataset["Vmag"], dtype=float)
14# B-V color index
15bmv = np.array(dataset["B-V"], dtype=float)
16# Paralax
17paralax = np.array(dataset["Plx"].notna().notnull(), dtype=float)
18paralax = paralax[paralax > 0.1]
19# Compute distance only if parallax is not zero
20distance = 1000 / paralax # unit: parsec
21# Absolute magnitude
22absoluteMag = apparentMag - 5 * np.log10(distance / 10)
23
24# Plot the KDE of apparent magnitude, B-V color index, distance and absolute magnitude
25figure, axis = plt.subplots(2, 2, figsize=(10, 10))
26axis[0, 0].set_title("KDE of apparent magnitude")
27axis[0, 0].set_xlabel("Apparent magnitude")
28axis[0, 0].set_ylabel("Density")
29
30axis[0, 1].set_title("KDE of B-V color index")
31axis[0, 1].set_xlabel("B-V color index")
32axis[0, 1].set_ylabel("Density")
33
34axis[1, 0].set_title("KDE of distance")
35axis[1, 0].set_xlabel("Distance")
36axis[1, 0].set_ylabel("Density")
37
38axis[1, 1].set_title("KDE of absolute magnitude")
39axis[1, 1].set_xlabel("Absolute magnitude")
40axis[1, 1].set_ylabel("Density")
41
42axis[0, 0].hist(apparentMag, bins=100, density=True)
43axis[0, 1].hist(bmv, bins=100, density=True)
44axis[1, 0].hist(paralax, bins=100, density=True)
45axis[1, 1].hist(absoluteMag, bins=100, density=True)
46
47plt.show()
48
49# Close the file
50f.close()
51
hr_plot.py Raw
1import os
2import pandas as pd
3import numpy as np
4import matplotlib.pyplot as plt
5
6import utilities
7
8# Open the file "/hipparcos-voidmain.csv" for reading
9f = open("./hipparcos-voidmain.csv", "r")
10
11dataset = pd.read_csv(f, sep=",", header=0)
12
13# Isolate the data we want to plot
14inputData = np.array(dataset[["B-V", "e_B-V", "Vmag", "Plx", "e_Plx"]], dtype=float)
15
16# Filter the data
17# Get rid of the NaN values
18inputData = inputData[~np.isnan(inputData).any(axis=1)]
19# Apply the following conditions:
20# - e_B-V should be smaller than 0.1
21# - Plx should be larger than 0.1
22# - e_Plx should be smaller than 5
23inputData = inputData[(inputData[:, 1] < 0.1) & (inputData[:, 3] > 0.1) & (inputData[:, 4] < 5)]
24
25# Compute the distance
26distance = 1000 / inputData[:, 3] # unit: parsec
27# Compute the absolute magnitude
28absoluteMag = inputData[:, 2] - 5 * np.log10(distance / 10)
29bmv = inputData[:, 0]
30
31# Map B-V to RGB
32rgb = np.array([utilities.bv2rgb(item) for item in bmv])
33
34# Plot the Hertzsprung-Russell diagram
35plt.figure(figsize=(10, 10))
36plt.scatter(bmv, absoluteMag, s=0.05, c=rgb, alpha=0.75)
37plt.xlim(-0.5, 2.5)
38plt.ylim(-15, 15)
39plt.xlabel("B-V")
40plt.ylabel("Vmag")
41plt.title("Hertzsprung-Russell diagram")
42plt.gca().set_facecolor((0, 0, 0))
43plt.gca().invert_yaxis()
44plt.show()
45
46# Close the file
47f.close()
48
position_plot.py Raw
1import os
2import pandas as pd
3import numpy as np
4import matplotlib.pyplot as plt
5
6import utilities
7
8# Open the file "./hipparcos-voidmain.csv" for reading
9f = open("./hipparcos-voidmain.csv", "r")
10
11dataset = pd.read_csv(f, sep=",", header=0)
12
13# Get the position data
14x = np.array(dataset["RAdeg"], dtype=float)
15y = np.array(dataset["DEdeg"], dtype=float)
16
17# Get the magnitude data (will be use to determine the size of the point)
18apparentMag = np.array(dataset["Vmag"], dtype=float)
19# Convert apparentMag to size
20size = 10 ** (apparentMag / -2.5)
21
22# Get the color data (will be use to determine the color of the point)
23bmv = np.array(dataset["B-V"], dtype=float)
24# Map B-V to RGB
25rgb = np.array([utilities.bv2rgb(bv) for bv in bmv])
26
27# Plot the stars position distribution
28plt.scatter(x, y, s=size, c=rgb, alpha=0.75)
29plt.title("Stars position")
30plt.xlabel("RAdeg")
31plt.ylabel("DEdeg")
32plt.gca().set_facecolor('black')
33plt.gca().invert_xaxis()
34plt.show()
35
36# Close the file
37f.close()
38
utilities.py Raw
1# Get the rgb color from B-V color
2# More info: https://wikipedia.org/wiki/Color_index
3# Function from https://stackoverflow.com/questions/21977786/star-b-v-color-index-to-apparent-rgb-color
4# By AymericG
5def bv2rgb(bv):
6 if bv < -0.40: bv = -0.40
7 if bv > 2.00: bv = 2.00
8 r = 0.0
9 g = 0.0
10 b = 0.0
11 if -0.40 <= bv<0.00:
12 t=(bv+0.40)/(0.00+0.40)
13 r=0.61+(0.11*t)+(0.1*t*t)
14 elif 0.00 <= bv<0.40:
15 t=(bv-0.00)/(0.40-0.00)
16 r=0.83+(0.17*t)
17 elif 0.40 <= bv<2.10:
18 t=(bv-0.40)/(2.10-0.40)
19 r=1.00
20 if -0.40 <= bv<0.00:
21 t=(bv+0.40)/(0.00+0.40)
22 g=0.70+(0.07*t)+(0.1*t*t)
23 elif 0.00 <= bv<0.40:
24 t=(bv-0.00)/(0.40-0.00)
25 g=0.87+(0.11*t)
26 elif 0.40 <= bv<1.60:
27 t=(bv-0.40)/(1.60-0.40)
28 g=0.98-(0.16*t)
29 elif 1.60 <= bv<2.00:
30 t=(bv-1.60)/(2.00-1.60)
31 g=0.82-(0.5*t*t)
32 if -0.40 <= bv<0.40:
33 t=(bv+0.40)/(0.40+0.40)
34 b=1.00
35 elif 0.40 <= bv<1.50:
36 t=(bv-0.40)/(1.50-0.40)
37 b=1.00-(0.47*t)+(0.1*t*t)
38 elif 1.50 <= bv<1.94:
39 t=(bv-1.50)/(1.94-1.50)
40 b=0.63-(0.6*t*t)
41 return (r, g, b)
42
43# Get the temperature from B-V color
44# More info: https://wikipedia.org/wiki/Color_index
45def bv2temp(bv):
46 temp=4600.0*(1.0/(0.92*bv+1.7)+1.0/(0.92*bv+0.62))
47 return temp