mercredi 29 juin 2016

No color when I make python scatter color plot using third variable to define color


I try to make colorful scatter plot using third variable to define color. It is simple to use the following code:

plt.scatter(mH, mA, s=1, c=mHc)
plt.colorbar()
plt.show()

But I do not have many choices to modify the frame of the plot. I am trying the following code to make colorful scatter plot, at the same time I try to optimize the frame of the plot:

import numpy as np
import math
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

fig, ax = plt.subplots()

cax = ax.scatter(mH,mA,s=0.5,c=mHc) ### mH, mA, mHC are the dataset
fig.colorbar(cax)
minor_locator1 = AutoMinorLocator(6)
minor_locator2 = AutoMinorLocator(6)
ax.xaxis.set_minor_locator(minor_locator1)
ax.yaxis.set_minor_locator(minor_locator2)
ax.tick_params('both', length=10, width=2, which='major')
ax.tick_params('both', length=5, width=2, which='minor')
ax.set_xlabel(r'$m_H$')
ax.set_ylabel(r'$m_A$')
ax.set_xticks([300,600,900,1200,1500])
ax.set_yticks([300,600,900,1200,1500])

plt.savefig('mH_mA.png',bbox_inches='tight')
plt.show()

But the plot I got is black-white. I believe the problem lies in this sentence fig.colorbar(cax), but I do not have much idea how to correct it. Anyone can offer me some idea to approach this issue. Thanks.enter image description here


Aucun commentaire:

Enregistrer un commentaire