plotly를 이용해서 산점도를 그릴 때 추세선(trendline)을 넣고 싶은 경우가 있다. 추세선을 넣어주면 데이터의 장기적인 증감 추세를 좀 더 한 눈에 볼 수 있기 때문이다. 추세선을 넣는 방법은 매우 간단하다.
px.scatter(df, x="total_bill", y="tip", trendline="ols")
그런데 여기서 에러가 나는 분들이 있을 것이다. 아마도 statsmodels 모듈과 관련된 에러일 것이다. 이 경우에는 statsmodels를 설치해주면 된다.
pip install statsmodels
만약 추세선의 색상을 별도로 지정하고 싶을 때는 trendline_color_override를 설정해주면 된다.
px.scatter(df, x="total_bill", y="tip", trendline="ols", trendline_color_override="red")
참고자료
[1] https://towardsdatascience.com/scatter-plots-with-plotly-express-1b7f5579919b
'코딩 > python' 카테고리의 다른 글
[python] 파이썬 리스트에 최대 몇 개의 요소가 들어갈 수 있을까? (10) | 2022.04.19 |
---|---|
[python] UnicodeDecodeError: 'cp949' codec can't decode byte 0xed in position 135: illegal multibyte sequence 에러 해결법 (0) | 2022.04.18 |
[python+opencv] ip 카메라(cctv)로부터 영상 받기 (2) | 2022.03.31 |
[python+plotly] 그래프 x축, y축 값 범위 설정하기 (0) | 2022.03.18 |
[python] 파이썬스럽게 코딩하는 법, PEP8 정리 (4) | 2022.03.15 |
[python] SyntaxError: Non-ASCII character '\xec' 에러 해결법 (0) | 2022.01.14 |
[python] 파이썬 에러 종류 정리(SyntaxError, TypeError, IndexError 등) (2) | 2021.06.30 |
[python] python 환경 변수 설정하기 (AppData 폴더가 왜 없지 하시는 분들을 위해) (2) | 2021.05.25 |