-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_graph.py
More file actions
35 lines (27 loc) · 780 Bytes
/
plot_graph.py
File metadata and controls
35 lines (27 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# output a plot/graph to pipeline output
#
import os
# Set the HOME or XDG_CONFIG_HOME environment variable to a valid directory
os.environ['HOME'] = '/tmp'# or any valid directory path
# Continue with your imports and code
import matplotlib.pyplot as plt
import numpy as np
import base64
from io import BytesIO
# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create a plot
plt.plot(x, y)
# Add labels and title
plt.xlabel('X-axis (radians)')
plt.ylabel('Y-axis (sin(x))')
plt.title('Sine Wave')
# Save the plot to a BytesIO object
buffer = BytesIO()
plt.savefig(buffer, format='png')
plt.close()
# Convert the plot to base64
buffer.seek(0)
image_base64 = base64.b64encode(buffer.read()).decode('utf-8')
output = image_base64 + "\n\n\n" + image_base64