Home > OS >  Problem with minor thicks and color bar in matplotlib
Problem with minor thicks and color bar in matplotlib

Time:12-09

This is cod for plotting. Here I have two problems.

import matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


#### part where data are loaded and defined######
tab1 = pd.read_table("tab1.txt", delim_whitespace=True) 
tab2 = pd.read_table("tab2.txt", delim_whitespace=True)

delen = (tab1['val2'] / tab1['val3']) *10**9
dist = tab1['val1']
size = abs(tab1['val4'])



m_Es_S0s = tab2['m1'][tab2['#type']==1]
r_Es_S0s = tab2['r1'][tab2['#type']==1]

m_dEs_dS0s = tab2['m1'][tab2['#type']==2]
r_dEs_dS0s = tab2['r1'][tab2['#type']==2]

m_dSphs = tab2['m1'][tab2['#type']==3]
r_dSphs =   tab2['r1'][tab2['#type']==3]

m_Nuclear_SC = tab2['m1'][tab2['#type']==4]
r_Nuclear_SC = tab2['r1'][tab2['#type']==4]

m_GCs_UCDs_cEs = tab2['m1'][tab2['#type']==5]
r_GCs_UCDs_cEs = tab2['r1'][tab2['#type']==5]

m_YMCs = tab2['m1'][tab2['#type']==7]
r_YMCs = tab2['r1'][tab2['#type']==7]


#####part related to figure #########

fig1 = plt.figure(figsize=(10,8),dpi=100)
ax = plt.subplot()
ax.tick_params(axis='both', which='both', direction="in")


plt.xticks(fontsize=14) 
plt.yticks(fontsize=14)
plt.xscale('log')
plt.yscale('log')


plt.scatter(delen ,delen/(2*3.141592653*size**2), marker='o', s=80, c=dist, cmap='Greys_r', alpha=0.9, norm=matplotlib.colors.LogNorm(), edgecolors='darkorchid', linewidth=0.5)  


a1=plt.scatter(m_Es_S0s  ,m_Es_S0s/(2*3.141592653*r_Es_S0s**2), marker='o', facecolors='none', edgecolors='mediumblue', linewidth=0.5, s=20)  
a2=plt.scatter(m_dEs_dS0s  ,m_dEs_dS0s/(2*3.141592653*r_dEs_dS0s**2), marker='o', facecolors='none', edgecolors='lightgreen', linewidth=0.5, s=20) 
#a3=plt.scatter(m_dSphs  ,m_dSphs/(2*3.141592653*r_dSphs**2),   marker='o', facecolors='none', edgecolors='red', linewidth=0.5, s=20) 
a4=plt.scatter(m_Nuclear_SC  ,m_Nuclear_SC/(2*3.141592653*r_Nuclear_SC**2),    marker='o', facecolors='none', edgecolors='dodgerblue', linewidth=0.8, s=20) 
#a5=plt.scatter(m_GCs_UCDs_cEs  ,m_GCs_UCDs_cEs/(2*3.141592653*r_GCs_UCDs_cEs**2), marker='o', facecolors='none', edgecolors='dimgrey', linewidth=0.5, s=20) 
a6=plt.scatter(m_YMCs  ,m_YMCs/(2*3.141592653*r_YMCs**2),  marker='o', facecolors='none', edgecolors='olive', linewidth=0.7, s=20)  





plt.clim(1.8,6.8)
cb = plt.colorbar(pad=0.004)
cb.set_label(label='dist', size='medium', weight='bold')
cb.ax.tick_params(labelsize='large',direction='in')





plt.ylabel('yaxis', fontsize=18)  
plt.xlabel('xaxis', fontsize=18)

plt.show()

Resulting plot looks like this:

check fig1

But, after uncommenting a3 and a5 (so, including more data points on the plot) I am losing all minor ticks on my plot. Figure looks like this

fig2

This is first problem why I am losing minor ticks I would like to keep them. Also I would like to keep all markers .... 10^5,10^6,10^7 ......

Another problem is that color bar does not change color. You can notice that my cmap='Greys_r' and points on the plot are ok, but color bar keeps viridis all the time. How to change color bar to Greys_r?

Tab1 and Tab2 are here: https://www.dropbox.com/s/gwj72blzallqjl5/tab1.txt?dl=0 https://www.dropbox.com/s/mj4fr8hetsb45eo/tab2.txt?dl=0

CodePudding user response:

Try this, it seems to work.

import matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#### part where data are loaded and defined######
tab1 = pd.read_table("tab1.txt", delim_whitespace=True)
tab2 = pd.read_table("tab2.txt", delim_whitespace=True)

delen = (tab1['val2'] / tab1['val3']) *10**9
dist = tab1['val1']
size = abs(tab1['val4'])



m_Es_S0s = tab2['m1'][tab2['#type']==1]
r_Es_S0s = tab2['r1'][tab2['#type']==1]

m_dEs_dS0s = tab2['m1'][tab2['#type']==2]
r_dEs_dS0s = tab2['r1'][tab2['#type']==2]

m_dSphs = tab2['m1'][tab2['#type']==3]
r_dSphs =   tab2['r1'][tab2['#type']==3]

m_Nuclear_SC = tab2['m1'][tab2['#type']==4]
r_Nuclear_SC = tab2['r1'][tab2['#type']==4]

m_GCs_UCDs_cEs = tab2['m1'][tab2['#type']==5]
r_GCs_UCDs_cEs = tab2['r1'][tab2['#type']==5]

m_YMCs = tab2['m1'][tab2['#type']==7]
r_YMCs = tab2['r1'][tab2['#type']==7]


#####part related to figure #########

fig1 = plt.figure(figsize=(10,8),dpi=100)
ax = plt.subplot()
ax.tick_params(axis='both', which='both', direction="in")

plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.xscale('log')
plt.yscale('log')


cc = plt.scatter(delen ,delen/(2*3.141592653*size**2), marker='o', s=80, c=dist, cmap='Greys_r', alpha=0.9, norm=matplotlib.colors.LogNorm(), edgecolors='darkorchid', linewidth=0.5)



a1=plt.scatter(m_Es_S0s  ,m_Es_S0s/(2*3.141592653*r_Es_S0s**2), marker='o', facecolors='none', edgecolors='mediumblue', linewidth=0.5, s=20)
a2=plt.scatter(m_dEs_dS0s  ,m_dEs_dS0s/(2*3.141592653*r_dEs_dS0s**2), marker='o', facecolors='none', edgecolors='lightgreen', linewidth=0.5, s=20)
a3=plt.scatter(m_dSphs  ,m_dSphs/(2*3.141592653*r_dSphs**2),   marker='o', facecolors='none', edgecolors='red', linewidth=0.5, s=20)
a4=plt.scatter(m_Nuclear_SC  ,m_Nuclear_SC/(2*3.141592653*r_Nuclear_SC**2),    marker='o', facecolors='none', edgecolors='dodgerblue', linewidth=0.8, s=20)
a5=plt.scatter(m_GCs_UCDs_cEs  ,m_GCs_UCDs_cEs/(2*3.141592653*r_GCs_UCDs_cEs**2), marker='o', facecolors='none', edgecolors='dimgrey', linewidth=0.5, s=20)
a6=plt.scatter(m_YMCs  ,m_YMCs/(2*3.141592653*r_YMCs**2),  marker='o', facecolors='none', edgecolors='olive', linewidth=0.7, s=20)


plt.clim(1.8,6.8)
cb = plt.colorbar(cc,pad=0.004)
cb.set_label(label='dist', size='medium', weight='bold')
#cb.ax.tick_params(labelsize='large',direction='in')

import matplotlib.ticker
## set y ticks
y_major = matplotlib.ticker.LogLocator(base = 10, numticks = 15)
ax.yaxis.set_major_locator(y_major)
y_minor = matplotlib.ticker.LogLocator(base = 10, subs = np.arange(1.0, 10.0) * 0.1, numticks = 20)
ax.yaxis.set_minor_locator(y_minor)
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

x_major = matplotlib.ticker.LogLocator(base = 10, numticks = 15)
ax.xaxis.set_major_locator(x_major)
x_minor = matplotlib.ticker.LogLocator(base = 10, subs = np.arange(1.0, 10.0) * 0.1, numticks = 20)
ax.xaxis.set_minor_locator(x_minor)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

plt.ylabel('yaxis', fontsize=18)
plt.xlabel('xaxis', fontsize=18)
#plt.savefig("out1.png")
plt.show()

Output fig is here. enter image description here

  • Related