基于plotly數據可視化
The amount of data in the world is growing every second. From sending a text to clicking a link, you are creating data points for companies to use. Insights that can be drawn from this collection of data can be extremely valuable. Every business has their own storage of data that they need to examine. One of the most important ways this examination is done is by visualizing the data.
噸他達到世界數據的增長每一秒。 從發送文本到單擊鏈接,您正在創建供公司使用的數據點。 從這些數據收集中得出的見解可能非常有價值。 每個企業都有自己需要檢查的數據存儲。 進行此檢查的最重要方法之一是可視化數據。
為什么要可視化數據? (Why Visualize Data?)
Simply put — “a picture is worth a thousand words”. In the entire history of business, data visualization has remained a necessary component. The reason it is so necessary is ultimately because we are visual creatures. Why else do you think a majority of us would prefer to watch a movie adaptation of a book than read the book itself? In terms of business presentations, a graph or chart of sales data may prove more insightful than just plain text. It is easy to draw insights from visual mediums rather than word documents.
簡而言之-“ 一張圖片值得一千個字 ”。 在整個業務歷史中,數據可視化仍然是必不可少的組成部分。 之所以如此必要,是因為我們最終是視覺生物。 您為什么還認為我們大多數人寧愿看電影改編電影也不愿看書本身? 就業務演示而言,銷售數據的圖形或圖表可能會證明比純文本更具洞察力。 從視覺媒介而不是單詞文檔中汲取見解很容易。
By visualizing the data you are making the data more accessible to a wider audience. This can help draw more insights because someone else might have an insight or two that you may never have thought of. The more people that see your visualization, then the more insights can potentially be made.
通過可視化數據,您可以使更廣泛的受眾更容易訪問數據。 這可以幫助您獲得更多的見解,因為其他人可能有您可能從未想過的一兩個見識。 看到您的可視化內容的人越多,則可以潛在地獲得更多的見解。

Visualizations also play a key role when presenting to crucial decision makers such as board members or shareholders. As you are constructing your numerous graphs and plots to highlight key data points, the visuals you decide to make can help push these decision makers in one direction or another. If the data visuals are presented with a select narrative in mind, then these decision makers will be inclined to make specific decisions based on your presentation.
在向關鍵決策者(例如董事會成員或股東)展示時,可視化也起著關鍵作用。 在構建大量圖形和圖表以突出顯示關鍵數據點時,您決定制作的視覺效果可以幫助將這些決策者推向一個方向或另一個方向。 如果在呈現數據視覺效果時考慮了特定的敘述,那么這些決策者將傾向于根據您的呈現做出特定的決策。
數據可視化工具 (Tools for Data Visualization)
Pie charts, bar charts, line graphs, and so on are all effective visuals when presenting data. These visuals are the tried and true forms for data presentation and we have made it even easier to create them. What we once use to do by hand can now be done with a couple of clicks on a computer.
餅圖,條形圖,折線圖等都是呈現數據時的有效視覺效果。 這些視覺效果是經過驗證的真實數據展示形式,我們使創建它們變得更加容易。 現在,我們曾經手動執行的操作現在只需在計算機上單擊幾下即可完成。
Nowadays, we have access to multiple programs to construct beautiful looking charts and graphs. These tools range from more technically based applications of visualization like Python’s Matplotlib or Plotly to more user-friendly ones like Tableau or Microsoft Power BI. Data visualizations tools are now more accessible than ever before.
如今,我們可以使用多個程序來構建美觀的圖表。 這些工具的范圍從更基于技術的可視化應用程序(例如Python的Matplotlib或Plotly)到更加用戶友好的工具(如Tableau或Microsoft Power BI) 。 數據可視化工具現在比以往任何時候都更易于訪問。

Within the realm of Python programming, there are many different libraries you could use to craft data visualizations. These libraries include, but are not limited, to Altair, Seaborn, and Plotly. There is no superior Python library because it all depends on what you are comfortable with and the problem or data you are trying to visualize.
在Python編程領域內,可以使用許多不同的庫來制作數據可視化。 這些庫包括但不限于Altair , Seaborn和Plotly 。 沒有出色的Python庫,因為這一切都取決于您是否熟悉什么以及要可視化的問題或數據。
學習如何使用情節 (Learning How to Use Plotly)
One of the tools we mentioned before is called Plotly. Plotly is a graphing and plotting library in Python similar to Matplotlib. The difference between the two is the fact that Plotly creates dynamically, interactive charts and graphs.
我們之前提到的工具之一稱為Plotly 。 Plotly是Python中的繪圖和繪圖庫,類似于Matplotlib。 兩者之間的區別在于Plotly創建動態的交互式圖表的事實。
一個簡單的業務問題 (A Simple Business Problem)
To get started with Plotly, we will need data to graph or plot first. So let’s say for example you work for a business that sells clothing. They want you to chart the sales for their shirts and jeans over the course of one year and have provided you with the data to do so. This problem will help us begin working with Plotly.
要開始使用Plotly,我們將需要首先繪制圖形或繪圖的數據。 舉例來說,假設您為一家銷售服裝的公司工作。 他們希望您繪制一年中襯衫和牛仔褲的銷售圖表,并提供了相關數據。 這個問題將幫助我們開始使用Plotly。
劇情安裝 (Installing Plotly)
In order to begin, we must first install Plotly by using the following command in your terminal:
首先,我們必須首先在終端中使用以下命令來安裝Plotly:
$ pip install plotly
Or if you have Anaconda installed:
或者,如果您安裝了Anaconda :
$ conda install -c plotly plotly
導入 (Importing Plotly)
Now that you have Plotly installed, let’s open a new file and start importing the necessary libraries for our data visualization example:
現在您已經安裝了Plotly,讓我們打開一個新文件,并開始為數據可視化示例導入必要的庫:
import plotly.express as px
import calendar as cal
import random
import pandas as pd
Here we are using plotly.express
, which is a module within Plotly that will quickly create graphs and charts for us.
在這里,我們使用plotly.express
,它是plotly.express
中的一個模塊,可以為我們快速創建圖形和圖表。
創建數據 (Creating the Data)
Since we are not actually given real data, we will have to create our own:
由于實際上并未提供真實數據,因此我們將必須創建自己的數據:
data = {'Months': [cal.month_name[i] for i in range(1,13)],
'Shirts': [round(random.gauss(100, 15)) for _ in range(12)],
'Jeans': [round(random.gauss(50, 20)) for _ in range(12)]}
Plotly works very well with Pandas DataFrames so we will store our newly created data into a DF:
Plotly與Pandas DataFrames配合得很好,因此我們會將新創建的數據存儲到DF中:
df = pd.DataFrame(data)
This new DF looks like this:
這個新的DF看起來像這樣:

情節的條形圖 (Plotly’s Bar Chart)
Now that we have our DF ready we can begin crafting our bar chart:
現在我們已經準備好DF,可以開始制作條形圖了:
fig = px.bar(df,
x='Months',
y=['Shirts','Jeans'])fig.show()
Here we are using the .bar()
method and inputting the DF of our data, and specifying the x and y axes. We are crafting a stacked bar chart by making a list for the columns: ‘Shirts
’ and ‘Jeans
’. Which we’ll display by calling fig.show()
.
在這里,我們使用.bar()
方法并輸入數據的DF,并指定x和y軸。 我們通過列出以下列的列表來制作堆疊式條形圖:“ Shirts
”和“ Jeans
”。 我們將通過調用fig.show()
顯示。

Success! That was simple enough. The cool thing about this Plotly chart is that you can start interacting with it by zooming in, panning, etc. But in regards to the overall chart, there are some things we would like to change to make this graph a little bit more descriptive like adding a title and renaming a few of the labels.
成功! 那很簡單。 關于該Plotly圖表,最酷的事情是您可以通過放大,平移等開始與之交互。但是對于整體圖表,我們需要進行一些更改以使該圖表更具描述性,例如添加標題并重命名一些標簽。
fig = px.bar(df,
x='Months',
y=['Shirts','Jeans'],
title='Total Monthly Item Sales',
labels={'variable': 'Item',
'value': 'Quantity Sold (in thousands)'})fig.show()
The difference between this code and the code before is the addition of the title=
and labels={}
argument. With these new arguments we are adding in a title for the chart and under the labels
we are basically using a dictionary to replace the two current labels.
此代碼與之前的代碼之間的區別是添加了title=
和labels={}
參數。 通過這些新參數,我們為圖表添加了一個標題,在labels
之下,我們基本上是使用字典來替換兩個當前標簽。

Now that the bar chart is properly labeled, we are basically finished with using Plotly for this data. But what if we wanted to do other kinds of charts or graphs in order to view different sides of the data?
現在,條形圖已正確標記,我們基本上已經完成了對數據使用Plotly的操作。 但是,如果我們想做其他種類的圖表來查看數據的不同方面怎么辦?
情節的線圖 (Plotly’s Line Graph)
Plotly allows us to create other types of visualizations too. We can easily create a line graph by using the code from before and just changing one thing:
通過Plotly,我們也可以創建其他類型的可視化。 我們可以使用之前的代碼輕松地創建折線圖,而只需更改一件事:
fig = px.line(df,
x='Months',
y=['Shirts','Jeans'],
title='Monthly Item Sales',
labels={'variable': 'Item',
'value': 'Quantity Sold (in thousands)'})fig.show()
All we did here was change px.bar
to px.line
. This now displays the following:
我們在這里所做的只是將px.bar
更改為px.line
。 現在將顯示以下內容:

Now we have a line graph! But wait there’s more…
現在我們有了折線圖! 但是,等等……
情節餅圖 (Plotly’s Pie Chart)
Let’s say we wanted to compare how many shirts were sold vs how many jeans were sold in the entire year.
假設我們要比較全年售出的襯衫數量與牛仔褲的銷售數量。
First, we must change our data to show the total sum of all sales for shirts and jeans:
首先,我們必須更改數據以顯示襯衫和牛仔褲的所有銷售總額:
pie_df = df[['Shirts','Jeans']].sum()
Here we’re just getting the sum of both Shirts
and Jeans
from the DF. Then, we will need to use px.pie()
using our new summed up DF.
在這里,我們只是從DF中獲取Shirts
和Jeans
的總和。 然后,我們需要使用新的匯總DF使用px.pie()
。
fig = px.pie(values=pie_df.values,
names=pie_df.index,
title="Sales Percentage in a Year")fig.show()
The argument values
is used to determine the sizes of each portion of the pie chart. The names
are the labels for each of the portions.
參數values
用于確定餅圖各部分的大小。 names
是每個部分的標簽。

Awesome! Now we have created three different types of visualizations for our data. But you don’t have to stop — there are more options available (see here for more) if you feel the need to continue experimenting with Plotly.
太棒了! 現在,我們為數據創建了三種不同類型的可視化。 但是您不必停下來-如果您有需要繼續嘗試Plotly的權利,可以使用更多選項( 有關更多信息, 請參見此處 )。
數據見解和結論 (Data Insights and Conclusions)
After visualizing our data, we would need to come to some sort of insight or conclusion based on the visuals. What can you tell based on these charts? Are there some obvious conclusions that can be drawn? What about some not so obvious ones?
可視化我們的數據后,我們需要根據視覺效果得出某種見解或結論。 您可以根據這些圖表看出什么? 是否可以得出一些明顯的結論? 那些不太明顯的東西呢?
Anyways, insights and conclusions are easier to see rather than read. If you are still wondering about the importance of visualizations, then just take a look back at the DF we created and compare it to any of the visuals we created with Plotly. Sometimes reading information is not as good as seeing the information.
無論如何,見解和結論更容易看到而不是閱讀。 如果您仍然想知道可視化的重要性,那么只需回顧一下我們創建的DF,并將其與我們使用Plotly創建的任何可視化圖像進行比較。 有時閱讀信息不如閱讀信息。
翻譯自: https://towardsdatascience.com/how-to-use-plotly-for-data-visualization-f3d62bbcfd92
基于plotly數據可視化
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/388164.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/388164.shtml 英文地址,請注明出處:http://en.pswp.cn/news/388164.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!