Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

2/22/2023

Histogram drawing using matplotlib by python code.

 Refer to code:

first one is draw histogram

second one is for drawing simple graph


drawing histogram

.

import matplotlib.pyplot as plt

# Data to plot
data = [(40, 1.646054384000001), (233, 3.0769193350000013), (221, 2.6460548819999996),
(214, 2.3542021680000005), (322, 2.726835301999998), (94, 1.201160183999999),
(193, 2.501363478000002), (171, 1.3009034040000031), (595, 5.574669749999998),
(248, 2.455411452)]

# Separate the data into two lists for word length and processing time
word_lengths = [d[0] for d in data]
processing_times = [d[1] for d in data]

# Plot the histogram
plt.hist(processing_times, bins=5)

# Add labels and title
plt.xlabel("Processing Time")
plt.ylabel("Frequency")
plt.title("Histogram of Processing Time")

# Show the plot
plt.show()

..


drawing graph

.

import matplotlib.pyplot as plt

# Data to plot
data = [(40, 1.646054384000001), (233, 3.0769193350000013), ..]

word_lengths = [d[0] for d in data]
processing_times = [d[1] for d in data]

plt.bar(word_lengths, processing_times)
plt.xlabel("Word Length")
plt.ylabel("Processing Time")
plt.title("Histogram of Word Lengths vs. Processing Times")
plt.show()

..



Thank you.πŸ™‡πŸ»‍♂️

www.marearts.com

6/26/2021

matplotlib plt to cv2

This example code is based on plt.pie drawing.

But you can apply any drawing way, just refer to how to be converted plt.fig 2 Numpy(cv2).


..

    import matplotlib.pyplot as plt
fig = plt.figure()
plt.pie(ratio, labels = mylabels, colors = mycolors) #, radius=180)
def get_img_from_fig(fig, dpi=180):
import io
buf = io.BytesIO()
fig.savefig(buf, format="png", dpi=dpi)
buf.seek(0)
img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
buf.close()
img = cv2.imdecode(img_arr, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

return img

plot_img_np = get_img_from_fig(fig)

cv2.namedWindow('palette')
cv2.imshow('palette', plot_img_np)
cv2.waitKey(0)

..



Thank you.

πŸ™‡πŸ»‍♂️