0

I have the following histogram: enter image description here

It was generated from the following piece of code:

hist = plt.hist((df2.to_numpy().flatten()), 70, facecolor = "blue", alpha = 0.75)
axes = plt.gca()
axes.set_xlim([0,15])
axes.set_ylim([0,65000])

I wanted to create a break in the y-axis in order to get ylim=(0,70)and ylim=(70, 60000).

Can anyone help?

Thanks in advance!

Zez
  • 125
  • 10

1 Answers1

0

Can you alter the example given by the user on this example? I think the fix there uses the matplotlib documentation example.

Edit: thought I should be more specific in my reply and give an example.

import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(10000) * 0.5 + 3. # making some random data to bin
bin_edges = np.linspace(0, 6, 7) # make the bins
bin_centres = np.linspace(0.5, 6.5, 6) # for plotting only
my_hist = np.histogram(data, bins = bin_edges)[0]

f, (ax, ax2) = plt.subplot(2,1,sharex = True, facecolor = 'w') # make the axes
ax.bar(bin_centres, my_hist) # plot on top axes
ax2.bar(bin_centres, my_hist) # plot on bottom axes
ax.set_ylim([4600,4900]) # numbers here are specific to this example
ax2.set_ylim([0, 500]) # numbers here are specific to this example
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_bottom() 
ax.tick_params(labeltop=False)
ax2.xaxis.tick_bottom()

enter image description here

Not too sure what the last lines did but when I did them they gave me a pretty good approximation of the example I linked to above. If you want to make it look prettier then there is more you can do, see the documentation for matplotlib.pyplot.bar and the example I linked.

Steven Thomas
  • 352
  • 2
  • 9
  • This seems more like a comment than an answer. – warped Feb 04 '20 at 17:45
  • @warped I agree, so I was already editting my response when you commented :) I hope my edits better and more like an answer. Please give me any feedback you think would help improve the answer further. – Steven Thomas Feb 04 '20 at 18:03
  • thank you! running this gives me a syntax error when you're defining "my_hist". I believe you missed on a ")" at the end of "bin_centres" line of code. When fixing this I get a new error that says - TypeError: bar() missing 1 required positional argument: 'height'. :\ – Zez Feb 05 '20 at 10:03
  • I found a solution to my problem using brokenazes :) – Zez Feb 05 '20 at 11:57
  • @user10934304 I had accidently pasted something in to that line when typing it out. The ax.bar bit shouldn't have been there. I have now updated the code. I'm glad you sorted your issue though – Steven Thomas Feb 06 '20 at 15:20