Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong Tracker values on a 3D histogram

Here is some code which displays a 3D histogram. However, the tracker in the lower-right corner does not correctly display the location of the mouse.

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

How can the code be fixed?

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

like image 874
unutbu Avatar asked Oct 16 '12 18:10

unutbu


People also ask

Can a histogram be 3D?

A 3D Histogram can be drawn by selecting two numeric or string variables as [Column 1] and [Column 2]. Optionally, one or more factors can be selected, in which case the program will display a list of all possible combinations of factor levels. The unchecked levels will be excluded from the plot.

How do you plot a 3D histogram in Python?

Use bar3d() method to plot 3D bars. To hide the axes use axis('off') class by name. To display the figure, use show() method.

What is Histogramming in computer science?

A histogram is a display of statistical information that uses rectangles to show the frequency of data items in successive numerical intervals of equal size. In the most common form of histogram, the independent variable is plotted along the horizontal axis and the dependent variable is plotted along the vertical axis.


1 Answers

First, you might change your xaxis formatter, currently it is throwing a lot of exceptions out to the interpreter when the mouse goes to the edges of the chart:

You might change this:

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

To something like this:

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

Next, I found there is an error in the actual mpl_toolkits.mplot3d

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

You'll 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

Note how self.axes.zz_dataLim.intervalx should be: self.axes.zz_dataLim.intervalz

Probably, you need to report these problems to the developer.

like image 152
Chris Zeh Avatar answered Oct 06 '22 18:10

Chris Zeh