1. 程式人生 > 其它 >在 Pycharm 等編輯器下使用 Python 視覺化神器 Plotly Express

在 Pycharm 等編輯器下使用 Python 視覺化神器 Plotly Express

http://liyangbit.com/pythonvisualization/Plotly-Express-IDE/ 在 Pycharm 等編輯器下使用 Python 視覺化神器 Plotly Express

Table of Contents

前些天公眾號釋出了一篇很受歡迎的文章

內容是關於 Plotly Express 的使用介紹。

在上面這篇文章裡,展示了 Plotly Express 在互動式視覺化方面的強大功能,很多童鞋表示很贊~~

但同時也遇到一些小問題,比如,上述程式碼是在 Jupyter Notebook 中執行的,有不少童鞋問到在 Pycharm 中如何執行?

剛開始遇到這個問題時,我基本回復的都是 “建議參考 Plotly 來進行修改”。

但估計很多童鞋還是不清楚怎麼執行,這裡,我提供一種方法,大家可以在 Pycharm 、 VScode 等 IDE中執行程式碼, 由於 Pycharm、VSCode 我用的相對較多,只測試了這兩種。 其他的 IDE,個人覺得也是差不多的,大家可以自己試試。

第一個示例

我們以 iris 資料集為例來演示,程式碼如下(.py 檔案):

# Code based on Python 3.x
# _*_ coding: utf-8 _*_
# Author: "陽哥"
# Website: http://liyangbit.com
# 公眾號: Python資料之道
# ID: PyDataLab

import plotly_express as px
import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)

iris = px.data.iris()

iris_plot = px.scatter(iris, x='sepal_width', y='sepal_length',
           color='species', marginal_y='histogram',
          marginal_x='box', trendline='ols')

plotly.offline.plot(iris_plot)

程式碼執行後,結果會出現在瀏覽器端,效果如下:

第二個示例

以 wind 資料集為例來演示,程式碼如下(.py 檔案):

# Code based on Python 3.x
# _*_ coding: utf-8 _*_
# Author: "陽哥"
# Website: http://liyangbit.com
# 公眾號: Python資料之道
# ID: PyDataLab

import plotly_express as px
import plotly
plotly.offline.init_notebook_mode(connected=True)

wind = px.data.wind()
wind_plot = px.bar_polar(wind, r="value", theta="direction", color="strength", template="plotly_dark",
            color_discrete_sequence= px.colors.sequential.Plotly[-2::-1])

plotly.offline.plot(wind_plot)

程式碼執行後,結果會出現在瀏覽器端,圖示如下:

看完上面的程式碼,是不是覺得 so easy, 趕緊動手試試吧。


對我的文章感興趣的朋友,可以關注我的微信公眾號「Python資料之道」(ID:PyDataLab),接收我的更新通知。