简体   繁体   中英

How to customize python matplotlib colorbar

I have generated a plot with colorbar in python.
Here is the code that I am currently using:

import openmc
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

sp1 = openmc.StatePoint('statepoint.550-20.h5')
tally1 = sp1.tallies[1]
flux1 = tally1.mean.ravel()
y = np.reshape(flux1, (200,200))

ax = plt.subplot(111)
Z = ax.imshow(y, cmap=plt.cm.viridis)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="10%", pad=0.05)

plt.colorbar(Z, cax=cax)
plt.show()

Which generates a plot shown in: 在此处输入图片说明

Now, my question is how do I customize the colorbar?

What I want to do is to set maximum and minimum value ( say 0.000000 to 0.000050 ) for the color scale and I want the plot to show color map as I define the scale, not the scale generated automatically from my array value y .

I am running python from mac terminal.

Any help is appreciated. Thanks.

通常, plt.clim(0.000000, 0.000050)应该可以工作。

I have solved it. Here is how am doing now,

import openmc
sp1 = openmc.StatePoint('statepoint.550-20.h5')
tally1 = sp1.tallies[1]
flux1 = tally1.mean.ravel()
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
y = np.reshape(flux1, (200,200)) 
ax = plt.subplot(111)
norm = matplotlib.colors.Normalize(vmin=0.000000,vmax=0.000025)
Z = ax.imshow(y, cmap=plt.cm.viridis, norm=norm )
plt.colorbar(Z)
plt.show()

Now I have (0.00000, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005) label in the scale, and the plot are showing according to the scale color value. Still, I need a guide on how to increase the label number from 6 to 11 as (0.00000, 0.000005, 0.00001, 0.000015, 0.00002, 0.000025, 0.00003, 0.000035, 0.00004, 0.000045, 0.00005) . Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM