Пользовательские метки и метки для Hexbin Colorbar в Pandas / Matplotlib - PullRequest
0 голосов
/ 27 апреля 2018

Как создать собственные метки и метки для цветовой панели графика Hexbin?

1 Ответ

0 голосов
/ 27 апреля 2018

Ключом к изменению цветовой панели является получение доступа к нему, затем использование локаторов и форматеров (в matplotlib.ticker ) для их изменения, и, наконец, обновление тиков после внесения изменений.

import matplotlib.pyplot as plt
import matplotlib.ticker as tkr 

# set the values of the main y axis and colorbar ticks 
ticklocs = tkr.FixedLocator( np.linspace( ymin, ymax, ystep ) )
ticklocs2 = tkr.FixedLocator( np.arange( zmin, zmax, zstep ) )

# set the label format for the main y axis, and colorbar
tickfrmt = tkr.StrMethodFormatter( yaxis_format )
tickfrmt2 = tkr.StrMethodFormatter( zaxis_format )

# must save a reference to the plot in order to retrieve its colorbar
hb = df.plot.hexbin(
    'xaxis', 'yaxis', C = 'zaxis',
    sharex = False
)

# get plot axes, ax[0] is main axes, ax[1] is colorbar
ax = plt.gcf().get_axes() 

# get colorbar
cbar = hb.collections[ 0 ].colorbar

# set main y axis ticks and labels
ax[ 0 ].yaxis.set_major_locator( ticklocs )
ax[ 0 ].yaxis.set_major_formatter( tickfrmt )

# set color bar ticks and labels
cbar.locator = ticklocs2 
cbar.formatter = tickfrmt2
cbar.update_ticks() # update ticks for changes to take place

(Эта проблема вызвала у меня некоторые существенные проблемы, поэтому я подумал, что поделюсь, как это сделать.)

...