App Programming/FastAPI
[FastAPI] 그래프 그리기
goatlab
2023. 7. 21. 10:21
728x90
반응형
SMALL
from fastapi import FastAPI, Response
import matplotlib.pyplot as plt
import io
app = FastAPI()
@app.get("/plot/")
async def get_plot():
# 데이터 생성
x = [1, 2, 3, 4, 5]
y = [10, 5, 7, 3, 8]
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Sample Plot')
ax.grid(True)
# 그래프를 이미지로 저장
buffer = io.BytesIO()
fig.savefig(buffer, format="png")
buffer.seek(0)
plt.close(fig)
# 이미지 데이터 반환
return Response(content=buffer.getvalue(), media_type="image/png")
uvicorn main:app --reload
728x90
반응형
LIST