docs/en/guides/analytics.md
This guide provides a comprehensive overview of three fundamental types of data visualizations: line graphs, bar plots, and pie charts. Each section includes step-by-step instructions and code snippets on how to create these visualizations using Python.
<p align="center"> <iframe loading="lazy" width="720" height="405" src="https://www.youtube.com/embed/tVuLIMt4DMY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen> </iframe><strong>Watch:</strong> How to generate Analytical Graphs using Ultralytics | Line Graphs, Bar Plots, Area and Pie Charts
</p>| Line Graph | Bar Plot | Pie Chart |
|---|---|---|
!!! example "Analytics using Ultralytics YOLO"
=== "CLI"
```bash
yolo solutions analytics show=True
# Pass the source
yolo solutions analytics source="path/to/video.mp4"
# Generate the pie chart
yolo solutions analytics analytics_type="pie" show=True
# Generate the bar plots
yolo solutions analytics analytics_type="bar" show=True
# Generate the area plots
yolo solutions analytics analytics_type="area" show=True
```
=== "Python"
```python
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
# Video writer
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"analytics_output.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
fps,
(1280, 720), # this is fixed
)
# Initialize analytics object
analytics = solutions.Analytics(
show=True, # display the output
analytics_type="line", # pass the analytics type, could be "pie", "bar" or "area".
model="yolo26n.pt", # path to the YOLO26 model file
# classes=[0, 2], # display analytics for specific detection classes
)
# Process video
frame_count = 0
while cap.isOpened():
success, im0 = cap.read()
if success:
frame_count += 1
results = analytics(im0, frame_count) # update analytics graph every frame
# print(results) # access the output
out.write(results.plot_im) # write the video file
else:
break
cap.release()
out.release()
cv2.destroyAllWindows() # destroy all opened windows
```
Analytics ArgumentsHere's a table outlining the Analytics arguments:
{% from "macros/solutions-args.md" import param_table %} {{ param_table(["model", "analytics_type"]) }}
You can also leverage different track arguments in the Analytics solution.
{% from "macros/track-args.md" import param_table %} {{ param_table(["tracker", "conf", "iou", "classes", "verbose", "device"]) }}
Additionally, the following visualization arguments are supported:
{% from "macros/visualization-args.md" import param_table %} {{ param_table(["show", "line_width"]) }}
Understanding when and how to use different types of visualizations is crucial for effective data analysis. Line graphs, bar plots, and pie charts are fundamental tools that can help you convey your data's story more clearly and effectively. The Ultralytics YOLO26 Analytics solution provides a streamlined way to generate these visualizations from your object detection and tracking results, making it easier to extract meaningful insights from your visual data.
To create a line graph using Ultralytics YOLO26 Analytics, follow these steps:
Analytics class with the type set to "line."Example:
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"ultralytics_analytics.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
fps,
(1280, 720), # this is fixed
)
analytics = solutions.Analytics(
analytics_type="line",
show=True,
)
frame_count = 0
while cap.isOpened():
success, im0 = cap.read()
if success:
frame_count += 1
results = analytics(im0, frame_count) # update analytics graph every frame
out.write(results.plot_im) # write the video file
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
For further details on configuring the Analytics class, visit the Analytics using Ultralytics YOLO26 section.
Using Ultralytics YOLO26 for creating bar plots offers several benefits:
Use the following example to generate a bar plot:
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"ultralytics_analytics.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
fps,
(1280, 720), # this is fixed
)
analytics = solutions.Analytics(
analytics_type="bar",
show=True,
)
frame_count = 0
while cap.isOpened():
success, im0 = cap.read()
if success:
frame_count += 1
results = analytics(im0, frame_count) # update analytics graph every frame
out.write(results.plot_im) # write the video file
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
To learn more, visit the Bar Plot section in the guide.
Ultralytics YOLO26 is an excellent choice for creating pie charts because:
Here's a quick example:
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"ultralytics_analytics.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
fps,
(1280, 720), # this is fixed
)
analytics = solutions.Analytics(
analytics_type="pie",
show=True,
)
frame_count = 0
while cap.isOpened():
success, im0 = cap.read()
if success:
frame_count += 1
results = analytics(im0, frame_count) # update analytics graph every frame
out.write(results.plot_im) # write the video file
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
For more information, refer to the Pie Chart section in the guide.
Yes, Ultralytics YOLO26 can be used to track objects and dynamically update visualizations. It supports tracking multiple objects in real-time and can update various visualizations like line graphs, bar plots, and pie charts based on the tracked objects' data.
Example for tracking and updating a line graph:
import cv2
from ultralytics import solutions
cap = cv2.VideoCapture("path/to/video.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
"ultralytics_analytics.avi",
cv2.VideoWriter_fourcc(*"MJPG"),
fps,
(1280, 720), # this is fixed
)
analytics = solutions.Analytics(
analytics_type="line",
show=True,
)
frame_count = 0
while cap.isOpened():
success, im0 = cap.read()
if success:
frame_count += 1
results = analytics(im0, frame_count) # update analytics graph every frame
out.write(results.plot_im) # write the video file
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
To learn about the complete functionality, see the Tracking section.
Ultralytics YOLO26 stands out from other object detection solutions like OpenCV and TensorFlow for multiple reasons:
For more detailed comparisons and use cases, explore our Ultralytics Blog.