Search code examples
pythonpandasmatplotlibplotastronomy

2 color bars in X and Y axis


I'm working on some Astronomy code in which I am plotting an Hr diagram, the problem is color is really important here and the X and Y axis both have to be labeled, but the axis is both on different scales. How would you suggest I do this? My current code is below.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data=pd.read_csv('Sagittarius_star_cloud.csv')
arr=data['bp_rp']
arr2=data['gp']
X=[]
Y=[]
for i in range(len(arr)):
    X.append(arr[i])
for i in range(len(arr2)):
    Y.append(arr2[i])
fig = plt.figure()
plt.ylim(max(Y), min(Y))
ax=fig.add_subplot(111)
plt.scatter(x=X, y=Y, c=X, cmap='Spectral_r')
plt.scatter(x=X, y=Y, c=Y, cmap='Spectral_r')
plt.colorbar(label="Star Color", orientation="horizontal")
plt.colorbar(label="Star Color", orientation="vertical")
plt.show()

Solution

  • I found a mapping that works for Hr diagrams(future reference)

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    csv_file='Hertzsprung-Russell3_diagram.csv'
    data=pd.read_csv(csv_file)
    arr=data['bp_rp']
    arr2=data['gp']
    X=[]
    Y=[]
    for i in range(len(arr)):
        X.append(arr[i])
    for i in range(len(arr2)):
        Y.append(arr2[i])
    fig = plt.figure()
    plt.ylim(max(Y), min(Y))
    ax=fig.add_subplot(111)
    plt.scatter(x=X, y=Y, c=X, cmap='RdYlBu_r')#cmap
    plt.colorbar(label="Star Color", orientation="horizontal")
    lhs,rhs=csv_file.split(".",1)
    plt.title(lhs)
    plt.show()