2011-06-28 11 views
7

He modificado el ejemplo scatter_hist.py encontrado here para tener dos conjuntos de datos a trazar.matplotlib scatter_hist con histtype stepfilled en el histograma

Me gustaría tener histogramas con tipo "relleno", pero de alguna manera si configuro el tipo "relleno" el histograma del eje Y (orientación = "horizontal") no está funcionando.

¿Hay alguna otra manera de hacer que el histograma parezca un estilo "relleno" o estoy haciendo algo mal?

Aquí está mi código con histtype = "bar" para mostrar la idea de lo que trato de hacer. Cambiarlo a

histtype="stepfilled" 

para obtener extraña histograma:

import numpy as np 
import matplotlib.pyplot as plt 

# the random data 
x = np.random.randn(1000) 
y = np.random.randn(1000) 

x_vals = [x] 
y_vals = [y] 
x_vals.append(np.random.randn(300)) 
y_vals.append(np.random.randn(300)) 

fig = plt.figure(1, figsize=(5.5,5.5)) 

from mpl_toolkits.axes_grid1 import make_axes_locatable 

colour_LUT = ['#0000FF', 
       '#00FF00'] 

# the scatter plot: 
xymax = np.max(np.fabs(x)) 
colors = [] 
axScatter = plt.subplot(111) 
for i in range(len(x_vals)): 
    colour = colour_LUT[i] 
    xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y)), xymax ]) 
    axScatter.scatter(x_vals[i], y_vals[i], color = colour) 
    colors.append(colour) 

axScatter.set_aspect(1.) 

# create new axes on the right and on the top of the current axes 
# The first argument of the new_vertical(new_horizontal) method is 
# the height (width) of the axes to be created in inches. 
divider = make_axes_locatable(axScatter) 
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter) 
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter) 

# make some labels invisible 
plt.setp(axHistx.get_xticklabels() + axHisty.get_yticklabels(), 
     visible=False) 

# now determine nice limits by hand: 
binwidth = 0.25 

lim = (int(xymax/binwidth) + 1) * binwidth 

bins = np.arange(-lim, lim + binwidth, binwidth) 
histtype = "bar" 
axHistx.hist(x_vals, bins=bins, histtype= histtype, color=colors) 
axHisty.hist(y_vals, bins=bins, orientation='horizontal',histtype= histtype, color=colors) 

# the xaxis of axHistx and yaxis of axHisty are shared with axScatter, 
# thus there is no need to manually adjust the xlim and ylim of these 
# axis. 

#axHistx.axis["bottom"].major_ticklabels.set_visible(False) 
for tl in axHistx.get_xticklabels(): 
    tl.set_visible(False) 
axHistx.set_yticks([0, 50, 100]) 

#axHisty.axis["left"].major_ticklabels.set_visible(False) 
for tl in axHisty.get_yticklabels(): 
    tl.set_visible(False) 
axHisty.set_xticks([0, 50, 100]) 

plt.draw() 
plt.show() 

gracias por la ayuda!

Editar:

Aquí son las imágenes que recibo en el entorno de ventanas con matplotlib 1.0.0. Con histtype = "bar" tengo esto:

bar histogram image http://i54.tinypic.com/wunjoi.png y con histtype = "stepfilled" tengo esto:

stepfilled histogram image http://oi56.tinypic.com/rstu1j.jpg

Respuesta

2

El documentation sólo menciona casos especiales para los datos de múltiples cuando se utiliza 'barra 'y' barstacked ', lo que supondría significa que esto no se implementa correctamente para los otros dos tipos. Cambiar su código para agregar múltiples histogramas en lugar de uno solo funcionó para mí:

histtype = "stepfilled" 
for i in xrange(len(x_vals)): 
    axHistx.hist(x_vals[i], bins=bins, histtype= histtype, color=colors[i]) 
    axHisty.hist(y_vals[i], bins=bins, orientation='horizontal',histtype= histtype, color=colors[i]) 
+0

Gracias por la respuesta Henning. ¿Cuál es su versión de matplotlib y lo está ejecutando en Windows? – Tedmu

+0

Estoy ejecutando matplotlib versión 1.0.0 en python 2.6.6 en Windows 7 – Henning