[Python] Can't Align Histogram Bin Edges with Chart Even When Using Numpy histogram_bin_edges

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I want the histogram edges to line up with the xticks of my chart. Other SO and answers I've read focus on calculating the bins and passing them. I've done this yet still can't get them to align: [​IMG]

    I've focused on the following lines to get this to work with no luck. In my_bins I tried converting it to a list (tolist()), and even with forcibly changing the xticks to match my_bins it doesn't align.

    my_bins = np.histogram_bin_edges(a=dataframe[data_cols].values.ravel(), bins='sqrt').tolist()

    axes_dict['histogram'].set_xticks(ticks=my_bins)

    axes_dict['histogram'].set_xticklabels(labels=my_bins)

    What should I try next?

    Here's my sample code:

    from matplotlib.figure import Figure
    from PIL import Image
    my_fig = Figure(**{'layout': 'constrained'})
    mosaic = {'mosaic': [['histogram']], 'gridspec_kw': {'wspace': 0.0, 'hspace': 0.0}}
    axes_dict = my_fig.subplot_mosaic(**mosaic)
    columns = ['label', 'obs1']
    data = [('', 74.03), ('', 73.995), ('', 73.988), ('', 74.002), ('', 73.992), ('', 74.009), ('', 73.995), ('', 73.985), ('', 74.008), ('', 73.998), ('', 73.994), ('', 74.004), ('', 73.983), ('', 74.006), ('', 74.012), ('', 74.0), ('', 73.994), ('', 74.006), ('', 73.984), ('', 74.0), ('', 73.988), ('', 74.004), ('', 74.01), ('', 74.015), ('', 73.982)]
    dataframe = pd.DataFrame(data, columns=columns)
    data_cols = ['obs1']
    my_bins = np.histogram_bin_edges(a=dataframe[data_cols].values.ravel(), bins='sqrt').tolist()
    # my_bins = [73.982, 73.9916, 74.0012, 74.0108, 74.0204, 74.03]
    axes_dict['histogram'].hist(**{'x': [74.03, 73.995, 73.988, 74.002, 73.992, 74.009, 73.995, 73.985, 74.008, 73.998, 73.994, 74.004, 73.983, 74.006, 74.012, 74.0, 73.994, 74.006, 73.984, 74.0, 73.988, 74.004, 74.01, 74.015, 73.982], 'bins': [73.982, 73.9916, 74.0012, 74.0108, 74.0204, 74.03], 'label': '', 'color': 'C0', 'zorder': 3.0, 'alpha': 0.5, 'histtype': 'step', 'align': 'left', 'orientation': 'vertical'})
    axes_dict['histogram'].set_xticks(ticks=my_bins)
    axes_dict['histogram'].set_xticklabels(labels=my_bins)
    my_fig.savefig('example_figure_for_stackoverflow.png')
    Image.open('example_figure_for_stackoverflow.png').show()

    Continue reading...

Compartilhe esta Página