How to Plot Multiple Gamma Distributions in Python with Matplotlib – A Step‑by‑Step Guide
This article walks through a common Python matplotlib visualization issue, showing how to read parameter data, generate multiple gamma distribution curves, add labels and legends, and produce polished plots, with complete code examples and explanations to help readers replicate the solution.
1. Introduction
Hello everyone, I am PiPi. A few days ago a member asked a Python visualization question in a group chat, showing the following problem:
The original code was:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
result_parameter_peak = pd.read_csv("result_parameter_peak.csv", encoding="utf_8_sig")
# Set canvas
fig = plt.figure(figsize=(20, 8))
axl = fig.add_subplot(1, 1, 1)
for i in range(len(result_parameter_peak)):
x = np.arange(0, 400, 1)
# Draw gamma curve
y661 = st.gamma.pdf(x, result_parameter_peak.iloc[i, 1], scale=result_parameter_peak.iloc[i, 2])
axl.plot(x, y661, 'r-.', label="α= 9.9028,β=10.4205")
axl.set_xlabel('Time')
axl.set_ylabel('Probility')
axl.set_title('分布')
plt.show()Running this produced only a single plot:
2. Solution
A contributor provided a new approach and sample code that iterates over the CSV rows using itertuples() and plots each gamma curve on the same figure:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
result_parameter_peak = pd.read_csv("result_parameter_peak.csv", encoding="utf_8_sig")
plt.figure()
for i, alpha, beta in result_parameter_peak.itertuples():
x = np.arange(0, 300, 1)
y661 = st.gamma.pdf(x, alpha, scale=beta)
plt.plot(x, y661, '-.')
plt.xlabel('Time')
plt.ylabel('Probility')
plt.title('分布')
plt.show()The resulting plot correctly displayed multiple gamma curves:
Another contributor added legends and labels to make the figure more informative:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
result_parameter_peak = pd.read_csv("result_parameter_peak.csv", encoding="utf_8_sig")
plt.figure()
for i, alpha, beta in result_parameter_peak.itertuples():
x = np.arange(0, 300, 1)
y661 = st.gamma.pdf(x, alpha, scale=beta)
plt.plot(x, y661, '-.', label="α:" + str(alpha) + "β:" + str(beta))
plt.xlabel('Time')
plt.ylabel('Probility')
plt.title('fenbu')
plt.legend()
plt.show()The final plot with annotations looks like this:
3. Summary
This article examined a common matplotlib plotting problem, provided detailed analysis and three complete code examples, and demonstrated how to generate and annotate multiple gamma distribution curves, enabling readers to solve the issue effectively.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
