0

I made a histogram on the Jupyter Notebook in Python. This histogram was performed by counting the number of equilateral triangles present in a sample and measuring its lateral size. Then the relative frequency was calculated. In this histogram I wanted to place a brokenaxes with this package, on the xx axis.

I wanted to eliminate empty spaces that range from 2.25 to 7.75. How could I do that? I had already tried using this package but the graphic always appeared unformatted. The goal is to reduce the x-axis to be able to make the graph larger, since the numbers on the x-axis appear very small. I would also like to know how to place a distribution line in the histogram.

I had already experimented with a Gaussian, but I don't think it fits the data well. I don't know if the ones that adapt best are gamma, log-normal or poisson and I don't know how to add the distribution line to the graph. I really appreciate your help.

I'll put here the code I used to make the graph and the Gaussian distribution:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
import matplotlib.ticker as tkr
import scipy, pylab
import locale
locale.setlocale(locale.LC_NUMERIC, "de_DE")
plt.rcParams['axes.formatter.use_locale'] = True

df=pd.DataFrame({'frequencia_relativa': {'0-0,25': 0.076, '0,25-0,50': 0.1591, 
                                         '0,50-30,75': 0.3030, '0,75-1,00': 0.2045,
                                         '1,00-1,25': 0.1288, '1,25-1,50': 0.1288, 
                                         '1,50-1,75': 0.0530, '1,75-2,00': 0.0000, 
                                         '2,00-2,25': 0.0076, '2,25-2,50': 0.0, 
                                         '2,50-2,75': 0.0, '2,75-3,00': 0.00, 
                                         '3,25-3,50': 0.00, '3,50-3,75': 0.00, 
                                         '3,75-4,00': 0.00, '4,00-4,25': 0.00, 
                                         '4,25-4,50': 0.00, '4,50-4,75': 0.00, 
                                         '4,75-5,00': 0.00, '5,25-5,50': 0.00, 
                                         '5,50-5,75': 0.00, '5,75-6,00': 0.00, 
                                         '6,00-6,25': 0.00, '6,25-6,50': 0.00, 
                                         '6,50-6,75': 0.00, '6,75-7,00': 0.00, 
                                         '7,00-7,25': 0.00, '7,25-7,50': 0.00, 
                                         '7,50-7,75': 0.00, '7,75-8,00': 0.0076}})
df=df.reindex(['0-0,25', '0,25-0,50', '0,50-30,75', '0,75-1,00', '1,00-1,25', '1,25-1,50', 
               '1,50-1,75', '1,75-2,00', '2,00-2,25', '2,25-2,50', '2,50-2,75', '2,75-3,00', 
               '3,25-3,50', '3,50-3,75', '3,75-4,00', '4,00-4,25', '4,25-4,50', '4,50-4,75', 
               '4,75-5,00', '5,25-5,50', '5,50-5,75', '5,75-6,00', '6,00-6,25', '6,25-6,50', 
               '6,50-6,75', '6,75-7,00', '7,00-7,25', '7,25-7,50', '7,50-7,75', '7,75-8,00'])
plt.rcParams["figure.figsize"] = [70,8]
fig, ax =plt.subplots()
ax.bar(x=df.index, height=df.frequencia_relativa, alpha=0.5, width=0.9)
ax.set_xlabel('Tamanho lateral do triângulo ($\mu m$)', fontsize=22)
ax.set_ylabel('Frequência relativa', fontsize=22)
x_axis = np.arange(0, 29, 0.001)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
ax.plot(x_axis, norm.pdf(x_axis,0.903,0.713), linewidth=3)
#plt.show()
plt.savefig('output.png', dpi=500, bbox_inches='tight')

enter image description here

  • You have pointed out a number of things you want to do with your graph. We usually expect one question per question. `how to add the distribution line to the graph` There is a [Matplotlib gallery example](https://matplotlib.org/gallery/statistics/histogram_features.html#sphx-glr-gallery-statistics-histogram-features-py) that fits a line to histogram data and adds that line to the plot - that should be a good starting point. If tha fit method in that example doesn't quite work just try other methods, experimentation works. – wwii Dec 28 '20 at 14:38
  • Related:[Fitting a histogram with python](https://stackoverflow.com/questions/7805552/fitting-a-histogram-with-python), ... [python: plotting a histogram with a function line on top](https://stackoverflow.com/questions/11315641/python-plotting-a-histogram-with-a-function-line-on-top) – wwii Dec 28 '20 at 14:41
  • Does [Python/Matplotlib - Is there a way to make a discontinuous axis?](https://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis) answer your question? – wwii Dec 28 '20 at 14:52
  • [Broken axis example](https://matplotlib.org/examples/pylab_examples/broken_axis.html) in Matplotlib documrntation. – wwii Dec 28 '20 at 14:59
  • `ax.plot(x_axis, norm.pdf(x_axis,0.903,0.713), linewidth=3)` --> `NameError: name 'x_axis' is not defined` - please fix. – wwii Dec 28 '20 at 15:04
  • @wwii Sorry, I already changed the code. Now I think there will no longer be an error. – Carmen González Dec 28 '20 at 15:14
  • @wwii My problem is that in all the examples I tried on the net, numbers appear on the x-axis. In my case, they are strings that give a range of values, so it always gives me an error and I can't solve it. I haven't found an example so far that had these characteristics on the xx axis. – Carmen González Dec 28 '20 at 15:16
  • @wwii The same problem applies when using ```brokenaxes```. Since the xx axis are strings, it does not cut the xx axis in the area where I want it. The graph appears unformatted. This example is different from all the previous ones I saw on Stack. – Carmen González Dec 28 '20 at 15:18
  • If you made another column of floats of bin edges/middles and used those numbers for calcs and making the graph then replaced the x-tick labels with the strings in `frequencia_relativa` - would [Change x-axis ticks to custom strings](https://stackoverflow.com/questions/43673884/change-x-axis-ticks-to-custom-strings) answer your question? ... Or [matplotlib strings as labels on x axis](https://stackoverflow.com/questions/7559242/matplotlib-strings-as-labels-on-x-axis)? – wwii Dec 28 '20 at 16:23
  • @wwii Sorry, I didn't notice. Could you write a response on how I can replace the xx axis of strings for numbers, please? I wanted the numbers to appear at the edges of the bars. I'm a Python novice and I never did that. So I could try to apply the distribution of the examples you gave. – Carmen González Dec 28 '20 at 20:54

0 Answers0