直方图填充指定的x/y值。

时间:2020-12-01 14:54:44

I have the following histogram plot:

我有以下直方图:

直方图填充指定的x/y值。

Now, I'm sure many of you will point me in the direction of the matplotlib libraries, and other resources, but for some reason what I have in mind, and subsequently, read from various different sources doesn't quite work with my axes histogram. (Correct me if I am wrong!)

现在,我相信你们中的很多人会把我指向matplotlib库和其他资源的方向,但是出于某种原因,我想到了,后来,从各种不同的来源中读到的,并不是用我的坐标轴直方图来做的。(如果我错了请纠正!)

My question is this:

我的问题是:

How do I hatch fill the histogram from the position on the line splits overlaid on the histograms in the (2,2,1) and (2,2,4) diagrams.

如何从(2,2,1)和(2,2,2,4)图中重叠的直方图上的位置填充直方图?

The x histogram (position 2,2,1) has a vline plotted at x = -0.222, and the y histogram has a hline plotted at y = 0.49.

x直方图(位置2,2,1)在x = -0.222处有一个vline,而y直方图在y = 0.49处有一个hline。

An example working code, modified for just any array from the original, is as follows:

对原数组中的任意数组进行修改的示例工作代码如下:

import numpy as np
import scipy
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab

X = 0.32, 0.41, 0.45, 0.53, -0.23,  0.34,  0.35, 0.47, 0.48, 0.33, 0.49, -0.10, -0.23,       0.45, 0.19
Y = 0.56, 0.67, 0.49, 0.61,  0.00, -0.02, -0.12, 0.12, 0.23, 0.44, 0.56,  0.13,  0.56, 0.67, 0.28

binsize = 0.1

min_x_data, max_x_data   = np.min(X), np.max(X)
num_x_bins               = np.floor((max_x_data - min_x_data) / binsize)

min_y_data, max_y_data   = np.min(Y), np.max(Y)
num_y_bins               = np.floor((max_y_data - min_y_data) / bin size)

fig = plt.figure(221)

axScatter = fig.add_subplot(223)
axScatter.scatter(X, Y)
axScatter.set_xlim(-2.0, 1.5)
axScatter.set_ylim(-2.0, 2.5)

axHistX = fig.add_subplot(221)
axHistX.set_xlim(-2.0, 1.5)
axHistX.set_ylim(0, 10)

axHistY = fig.add_subplot(224)
axHistY.set_xlim(0, 10)
axHistY.set_ylim(-2.0, 2.5)

axHistX.hist(X, num_x_bins, ec='0.3', fc='none', histtype='step')
axHistY.hist(Y, num_y_bins, ec='0.3', fc='none', histtype='step', orientation='horizontal')

axScatter.axhline(y=0.49, xmin=0, xmax=1, linestyle='-.',c='k')
axScatter.axvline(x=-0.222, ymin=0, ymax=1, linestyle='-.',c='k')
axHistX.axvline(x=-0.222, ymin=0, ymax=1, linestyle='-.',c='k')
axHistY.axhline(y=0.49, xmin=0, xmax=1, linestyle='-.',c='k')

plt.show()

1 个解决方案

#1


5  

So here my last try, but strongly suggest you to learn at the basics of python and matplotlib.

这里是我最后一次尝试,但强烈建议您学习python和matplotlib的基础知识。

def step_hist(ax, X, num_x_bins=10,               
              hatch_from= -2, hatch_till=0.5,
              orientation='h'):
    #make histogram by hand.
    hist, edges = np.histogram(X, bins=num_x_bins)
    #generate (x,y) points for a step-plot
    edges = np.repeat(edges, 2)
    hist = np.hstack((0, np.repeat(hist, 2), 0))
    #plot step_hist
    #indices where we want the  plot hatached
    fill_region  =(hatch_from<edges)&(edges<hatch_till) 
    #apply hatching my using fill_between.
    if orientation == 'h':    
        ax.fill_between(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(edges, hist, 'k')
    elif orientation == 'v':
        ax.fill_betweenx(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(hist, edges, 'k')

a, b = np.random.randn(500), np.random.randn(500)

ax_top = plt.subplot(221)
ax_right = plt.subplot(224)
ax_cent = plt.subplot(223)

ax_cent.scatter(a, b, c='k')
step_hist(ax_top, a)
step_hist(ax_right, a, orientation='v')

直方图填充指定的x/y值。

#1


5  

So here my last try, but strongly suggest you to learn at the basics of python and matplotlib.

这里是我最后一次尝试,但强烈建议您学习python和matplotlib的基础知识。

def step_hist(ax, X, num_x_bins=10,               
              hatch_from= -2, hatch_till=0.5,
              orientation='h'):
    #make histogram by hand.
    hist, edges = np.histogram(X, bins=num_x_bins)
    #generate (x,y) points for a step-plot
    edges = np.repeat(edges, 2)
    hist = np.hstack((0, np.repeat(hist, 2), 0))
    #plot step_hist
    #indices where we want the  plot hatached
    fill_region  =(hatch_from<edges)&(edges<hatch_till) 
    #apply hatching my using fill_between.
    if orientation == 'h':    
        ax.fill_between(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(edges, hist, 'k')
    elif orientation == 'v':
        ax.fill_betweenx(edges[fill_region], 
                        hist[fill_region], 0, 
                        color='none', edgecolor='k',
                        hatch='xxx')
        ax.plot(hist, edges, 'k')

a, b = np.random.randn(500), np.random.randn(500)

ax_top = plt.subplot(221)
ax_right = plt.subplot(224)
ax_cent = plt.subplot(223)

ax_cent.scatter(a, b, c='k')
step_hist(ax_top, a)
step_hist(ax_right, a, orientation='v')

直方图填充指定的x/y值。