Matplotlib Ref Hist
* * Matplotlib Reference](#)\n\n`hist()` is used to draw histograms, dividing data into several intervals (bins) and counting the frequency of data occurrences within each interval.\n\nHistograms are fundamental tools for exploring data distribution (central tendency, dispersion, skewness, etc.).\n\n## Function Definition\n\n### pyplot Interface\n\nmatplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, **kwargs)\n### Axes Interface\n\nAxes.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, **kwargs)\n## Parameter Description\n\n| Parameter | Type | Description |\n| --- | --- | --- |\n| x | array or sequence of arrays | Input data, multiple datasets can be passed in |\n| bins | int or sequence | Number of intervals or sequence of interval boundaries. Defaults to 'auto' method for automatic selection |\n| range | tuple | Data range (lower, upper), data outside the range is ignored |\n| density | bool | If True, draws a probability density histogram (total area equals 1) instead of frequency |\n| weights | array-like | Weight for each data point |\n| cumulative | bool or -1 | If True, draws a cumulative histogram; -1 means cumulative from largest to smallest |\n| histtype | str | Histogram type: 'bar' (default), 'barstacked' (stacked), 'step' (step line), 'stepfilled' (filled step) |\n| align | str | Bar alignment: 'mid' (centered, default), 'left' (left-aligned), 'right' (right-aligned) |\n| orientation | str | 'vertical' (vertical, default) or 'horizontal' (horizontal) |\n| rwidth | float | Relative width of bars, 1.0 means no gaps |\n| log | bool | If True, y-axis uses logarithmic scale |\n| color | color or list | Bar color |\n| label | str or list | Legend label |\n| stacked | bool | Whether to display stacked when there are multiple groups of data |\n\n> The return value of hist() is a 3-tuple: `(n, bins, patches)`. n is the frequency/density of each interval, bins are the interval boundaries, and patches are the drawn bar objects.\n\n* * *\n\n## Usage Examples\n\n### Example 1: Basic Histogram\n\n## Instance\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\n# Generate Normally Distributed Random Data\n\n np.random.seed(42)\n\n data = np.random.randn(1000)\n\nfig, ax = plt.subplots(layout='constrained')\n\n# Plot Histogram\n\n n, bins, patches = ax.hist(data, bins=30,\n\ncolor='steelblue', edgecolor='white')\n\n# Highlight the Bin with the Maximum Value\n\n max_idx = np.argmax(n)\n\n patches.set_facecolor('#e74c3c')\n\nax.set_title('Histogram of Normal Distribution')\n\n ax.set_xlabel('Value')\n\n ax.set_ylabel('Frequency')\n\n ax.axvline(x=0, color='red', linestyle='--', alpha=0.7)\n\n ax.grid(axis='y', alpha=0.3)\n\n plt.show()\n\n### Example 2: Multiple Data Comparison Histogram\n\n## Instance\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nnp.random.seed(42)\n\n# Three Datasets with Different Distributions\n\n data1 = np.random.normal(0,1,1000)# Standard Normal\n\n data2 = np.random.normal(2,1.5,800)# Mean=2, Standard Deviation=1.5\n\n data3 = np.random.normal(-1,0.5,600)# Mean=-1, Standard Deviation=0.5\n\nfig, ax = plt.subplots(figsize=(8,5), layout='constrained')\n\n# Compare Multiple Datasets Using Transparency and Different Colors\n\n ax.hist(data1, bins=30, alpha=0.6, color='steelblue',\n\n label='N(0, 1)', edgecolor='white')\n\n ax.hist(data2, bins=30, alpha=0.6, color='coral',\n\n label='N(2, 1.5)', edgecolor='white')\n\n ax.hist(data3, bins=30, alpha=0.6, color='mediumseagreen',\n\n label='N(-1, 0.5)', edgecolor='white')\n\nax.set_title('Comparing Multiple Distributions')\n\n ax.set_xlabel('Value')\n\n ax.set_ylabel('Frequency')\n\n ax.legend()\n\n ax.grid(axis='y', alpha=0.3)\n\n plt.show()\n\n### Example 3: Density Histogram + Fitted Curve\n\n## Instance\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nnp.random.seed(42)\n\n data = np.random.randn(1000)\n\nfig, ax = plt.subplots(figsize=(8,5), layout='constrained')\n\n# density=True Plot Probability Density (Instead of Frequency)\n\n ax.hist(data, bins=30, density=True, alpha=0.7,\n\n color='steelblue', edgecolor='white', label='Data Histogram')\n\n# Overlay Theoretical Normal Distribution Curve\n\nfrom scipy import stats\n\n x = np.linspace(-4,4,200)\n\n pdf = stats.norm.pdf(x, loc=0, scale=1)\n\n ax.plot(x, pdf,'r-', linewidth=2, label='Theoretical N(0,1) PDF')\n\nax.set_title('Density Histogram with PDF Curve')\n\n ax.set_xlabel('Value')\n\n ax.set_ylabel('Probability Density')\n\n ax.legend()\n\n ax.grid(axis='y', alpha=0.3)\n\n plt.show()\n\n### Example 4: Cumulative Histogram\n\n## Instance\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nnp.random.seed(42)\n\n data = np.random.randn(500)\n\nfig,(ax1, ax2)= plt.subplots(1,2, figsize=(10,4),\n\n layout='constrained')\n\n# Left Plot: Regular Histogram\n\n ax1.hist(data, bins=30, color='steelblue', edgecolor='white')\n\n ax1.set_title('Standard Histogram')\n\n ax1.set_xlabel('Value')\n\n ax1.set_ylabel('Frequency')\n\n# Right Plot: Cumulative Histogram\n\n ax2.hist(data, bins=30, cumulative=True, color='coral',\n\nedgecolor='white')\n\n ax2.set_title('Cumulative Histogram')\n\n ax2.set_xlabel('Value')\n\n ax2.set_ylabel('Cumulative Frequency')\n\nplt.show()\n\n### Example 5: Step Histogram\n\n## Instance\n\nimport matplotlib.pyplot as plt\n\nimport numpy as np\n\nnp.random.seed(42)\n\n data = np.random.exponential(scale=2, size=500)\n\nfig, ax = plt.subplots(layout='constrained')\n\n# histtype='stepfilled' Plot Filled Step Histogram\n\n ax.hist(data, bins=30, histtype='stepfilled',\n\n color='#3498db', alpha=0.6, edgecolor='black',\n\n linewidth=1, label='Stepfilled')\n\n# Overlay Step Line\n\n ax.hist(data, bins=30, histtype='step',\n\n color='black', linewidth=2, label='Step outline')\n\nax.set_title('Step Histogram (histtype)')\n\n ax.set_xlabel('Value')\n\n ax.set_ylabel('Frequency')\n\n ax.legend()\n\n ax.grid(axis='y', alpha=0.3)\n\n plt.show()\n\nprint("tutorial: step histogram displayed")\n\n* * *\n\n## Common Questions\n\n### How to choose the bins parameter?\n\nToo few will lose data features, too many will introduce noise.\n\nRules of thumb: `sqrt(n)` (square root rule), `log2(n)+1` (Sturges' rule), or simply use the default 'auto' for automatic selection.\n\n### What does density=True mean?\n\nInstead of returning the count, it makes the total area of the histogram equal to 1, forming an estimate of the probability density. This is suitable for comparison with a probability density function (PDF).\n\n[ Matplotlib Reference](#)
YouTip