matplotlib hist yerr
时间: 2023-10-14 08:15:23 浏览: 81
The `hist` function in Matplotlib does not have a `yerr` parameter. `yerr` is typically used to specify the error bars for a plot, which is not applicable for a histogram. However, you can use the `bar` function instead of `hist` to create a bar plot with error bars.
Here's an example:
```python
import numpy as np
import matplotlib.pyplot as plt
# Generate random data
data = np.random.normal(0, 1, 1000)
# Calculate histogram
counts, edges = np.histogram(data, bins=10)
# Calculate error bars
errors = np.sqrt(counts)
# Plot bar chart with error bars
plt.bar(edges[:-1], counts, yerr=errors, width=np.diff(edges), align='edge')
plt.show()
```
This code generates a random dataset, calculates the histogram using `np.histogram`, and then calculates the errors as the square root of the bin counts. The bar chart is then plotted using `plt.bar`, with the error bars specified using the `yerr` parameter.
阅读全文