Invalid Tracker values โ€‹โ€‹in 3D histogram

Here is some code that displays a three-dimensional histogram. However, the tracker in the lower right corner does not display the mouse location correctly.

The tracker says x = e when the mouse is clearly over c . And the tracker says z = 01-02 . What's up with that? (The value of the z-tracker seems to be controlled by the formatting of the y axis.)

How can I fix the code?

 import matplotlib.pyplot as plt import numpy as np import mpl_toolkits.mplot3d.axes3d as axes3d import matplotlib.dates as mdates import matplotlib.ticker as ticker import datetime as dt import random np.random.seed(0) fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection = '3d') cmap = plt.get_cmap('RdBu') event_labels = 'abcdefghij' events = range(len(event_labels)) label_map = dict(zip(events,event_labels)) dates = mdates.drange(dt.datetime(2012, 10, 1), dt.datetime(2012, 10, 10), dt.timedelta(days = 1)) events_list = [(random.choice(dates), random.choice(events)) for i in range(50)] event_array, date_array = zip(*events_list) # Much of the code below comes from # http://matplotlib.org/examples/mplot3d/hist3d_demo.html hist, xedges, yedges = np.histogram2d(date_array, event_array) elements = (len(xedges)-1) * (len(yedges)-1) xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25) xpos = xpos.flatten() ypos = ypos.flatten() zpos = np.zeros(elements) dx = 0.5 * np.ones_like(zpos) dy = dx.copy() dz = hist.flatten() ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b') xfmt = ticker.FuncFormatter(lambda x, pos: label_map[int(x)]) yfmt = mdates.DateFormatter('%m-%d') zfmt = ticker.FuncFormatter(lambda z, pos: str(z)) ax.w_xaxis.set_major_formatter(xfmt) ax.w_yaxis.set_major_formatter(yfmt) ax.w_zaxis.set_major_formatter(zfmt) ax.fmt_xdata = xfmt ax.fmt_ydata = yfmt ax.fmt_zdata = zfmt plt.show() 

enter image description here

+4
source share
1 answer

Firstly, you can change your xaxis format, currently it throws many exceptions to the interpreter when the mouse goes to the edges of the diagram:

You can change this:

  xfmt = ticker.FuncFormatter(lambda x, pos: label_map[int(x)]) 

Something like that:

 def xformatter(x,pos): try: val = label_map[int(x)] except: val = "None" return val ... xfmt = ticker.FuncFormatter(xformatter) 

Then I found that in the actual mpl_toolkits.mplot3d

there is a mistake

If you look at line 320 in \Lib\site-packages\mpl_toolkits\mplot3d\axis3d.py

You will find this typo:

 class YAxis(Axis): def get_data_interval(self): 'return the Interval instance for this axis data limits' return self.axes.xy_dataLim.intervaly class ZAxis(Axis): def get_data_interval(self): 'return the Interval instance for this axis data limits' return self.axes.zz_dataLim.intervalx 

Please note that self.axes.zz_dataLim.intervalx should be: self.axes.zz_dataLim.intervalz

You may need to report these issues to the developer.

+1
source

Source: https://habr.com/ru/post/1440113/


All Articles