plotly python_使用Plotly for Python时的基本思路

plotly python

I recently worked with Plotly for data visualization on predicted outputs coming from a Machine Learning Model.

我最近与Plotly合作,对来自机器学习模型的预测输出进行数据可视化。

The documentation I referred to : https://plotly.com/python/

我提到的文档: https : //plotly.com/python/

Here are few “google searches” I personally did while I was working on it:

以下是我在进行搜索时亲自进行的“ google搜索”:

Question : Which data viz library should I choose for interactive plots?Answer : Matplotlib, Seaborn, Plotly, Altair, and Bokeh were some of the answers. The reason I went with Plotly was because my project requirement was to get charts on a html page which is supported by Plotly. With Plotly, the charts are stored as .html files and can be zoomed in, zoomed out and also focused on a particular region.

问:我应该为交互式绘图选择哪个数据视图库?答案: Matplotlib,Seaborn,Plotly,Altair和Bokeh是其中的一些答案。 我选择Plotly的原因是因为我的项目要求是在Plotly支持的html页面上获取图表。 使用Plotly,可以将图表存储为.html文件,并且可以放大,缩小并且还可以将焦点放在特定区域上。

Question: Plotly Express or Plotly Graph Objects Answer : I started with Plotly Express but ended up using Plotly Graph Objects for the very reason that it provided me a lot of modifications to my existing code. There were more number of attributes which could provide better cosmetic changes to the charts.

问题:Plotly Express或Plotly Graph对象 回答:我从Plotly Express开始,但最终由于使用Plotly Graph Objects而对我的现有代码进行了大量修改,因此最终使用了Plotly Graph Objects。 有更多的属性可以为图表提供更好的外观更改。

关于图形对象 (In terms of Plotly Graph Objects)

import plotly.graph_objs as go

将go导入plotly.graph_objs

Question: How do I show two columns from my data set against time?Answer : choose your “mode” as lines and lines + markers.mode = ‘lines’ , can be set when you add your trace.

问题:如何显示数据集中随时间变化的两列? 答案:选择“模式”作为线和线+标记。mode='lines',可以在添加迹线时设置。

fig = go.Figure()fig.add_trace(go.Scatter(x=data[‘time’],y=data[‘Values_x’],mode=‘lines’))fig.add_trace(go.Scatter(x=data[‘time’],y=data[‘Values_y’],mode=‘lines+markers’))

fig = go.Figure()fig.add_trace(go.Scatter(x = data ['time'],y = data ['Values_x'],mode ='lines'))fig.add_trace(go.Scatter(x = data ['time'],y = data ['Values_y'],mode ='lines + marks'))

Question: What else I can add in my go.Scatter() function to modify my data points?Answer: Other attributes which can be used to modify the plots can be name : name of the data point which is being plotted, string valueshowlegend : If the legend should be visible or not, Boolean value with True or Falsemarker_color : if your mode is lines+markers or just markers you can give a specific color to it, it accepts RGBA, RGB, HSL, or a name of the color in a string valueline_width : determines the width of the line in your plot, accepts an int value line_color : the color of the linefont : one can choose the font from the font family available in plotly.

问题:我还可以在go.Scatter()函数中添加哪些内容来修改数据点? 答:其他可用于修改图的属性可以是name :要绘制的数据点的名称,字符串值showlegend :如果图例不可见,则布尔值为True或False marker_color :如果您的模式是线条+标记或只是标记,您可以为其指定特定的颜色,可以接受RGBA,RGB,HSL或字符串值中的颜色名称line_width :确定绘图中线条的宽度,接受一个int值line_color :线条字体的颜色:可以从可用的字体家族中选择字体。

Question: Once I choose my font family, how do I choose the font color and its size?Answer: Many attributes in plotly have an option of specifying a dict and further writing a key value pair in it.

问:选择字体系列后,如何选择字体颜色和字体大小?答案 :plotly中的许多属性都可以选择指定dict并在其中进一步编写键值对。

font=dict(family=’Times New Roman’,size=16,color=’red’)

font = dict(family ='Times New Roman',size = 16,color ='red')

Question: How do I put a hover label on my plots?Answer : Hover labels are kind of boxes which are visible when you move your cursor to a specific data point. They help the user in understanding the value and other details in your chart.

问:如何在图形上放置一个悬停标签?答案:悬停标签是将光标移到特定数据点时可见的一种框。 它们帮助用户了解图表中的值和其他详细信息。

hoverlabel=dict(bgcolor=’lightblue’,font=dict(family=’Times New Roman’,size=16),bordercolor=’black’),hovertemplate=’Booth Humidity<br>Probability: %{y}’)

hoverlabel = dict(bgcolor ='lightblue',font = dict(family ='Times New Roman',size = 16),bordercolor ='black'),hovertemplate ='Booth Humidity <br> Probability:%{y}')

bgcolor is the back ground color of the hover boxhovertemplate will contain basic html tags, it can be altered according to the style one wants to keep.

bgcolor是悬停框的背景hovertemplate将包含基本的html标签,可以根据您想要保留的样式进行更改。

Question: How do I make subplots in plotly?Answer: There are various kinds of subplots which we can make on plotly by specifying the number of rows and columns.

问题:我如何进行子图绘制? 答:通过指定行数和列数,可以在图上进行多种子图绘制。

fig.make_subplots(rows=3,column=1,vertical_spacing=0.5,horizontal_spacing=0.5)

fig.make_subplots(行= 3,列= 1,垂直间距= 0.5,水平间距= 0.5)

This will give you 3 subplots stacked together. It can be varied accordingly. Vertical spacing indicates the distance between the columns of the subplots specified whereas the Horizontal Spacing is between two rows of the subplots.

这将使您将3个子图堆叠在一起。 它可以相应地变化。 垂直间距表示指定的子图的各列之间的距离,而水平间距表示子图的两行之间。

Simply specify row and column number in each go.Scatter function. This way you can also keep multiple data points in the same subplot. (i.e two or more go.Scatter can have same row number and column number)

只需在每个go.Scatter函数中指定行号和列号。 这样,您还可以将多个数据点保留在同一子图中。 (即两个或多个go.Scatter可以具有相同的行号和列号)

Question: How do I make one subplot to be larger in size than my other two subplots?Answer: There is a very useful parameter called “specs” which is basically a 2D list inside the make_subplots() function where one can specify the colspan, rowspan or None. None indicates that no subplot will be drawn in that dimension and hence that is where your larger sized subplot goes.

问题:如何使一个子图的尺寸大于其他两个子图的尺寸? 答:有一个非常有用的参数称为“ specs” ,它基本上是make_subplots()函数中的2D列表,可以在其中指定colspan,rowspan或None。 None表示不会在该维度上绘制子图,因此这是您较大尺寸的子图所在的位置。

Question: Can I make a subplot with common axis?Answer: Plotly has a provision of making subplots with shared (common) xaxis and yaxis. shared_xaxes or shared_yaxes has to be set to true in the make_subplots() function

问题:我可以制作一个具有公共轴的子图吗? 答: Plotly提供了使用共享(公用)xaxis和yaxis制作子图的条件。 必须在make_subplots()函数中将shared_xaxes或shared_yaxes设置为true

Question: How do I give different xaxis title or yaxis title to each subplot?Answer: you can specify the axis title with the subplot number in the update_layout() function.

问题:如何给每个子图赋予不同的xaxis标题或yaxis标题? 答:您可以在update_layout()函数中用子图号指定轴标题。

Example- for subplot in the row 2 has the yaxis titled as yaxis2_title

示例-第2行中的子图的yaxis标题为yaxis2_title

Question: What is update_layout and what all attributes it holds?Answer: update_layout is the overall function to make the plot look presentable. One can specify the following in it:height : Height of the chartwidth : Width of the charttitle: The overall title of your chartshowgrid: the horizontal and vertical gridlines present in your chart plot_bgcolor: the color inside the plot paper_bgcolor: the color where the plot is present

问题:什么是update_layout?它具有什么所有属性? 答: update_layout是使绘图看起来更美观的整体功能。 一个可以指定在它下面的: 高度:图表宽度的高度图表标题的宽度图表showgrid的整体标题水平和垂直网格线存在于图表plot_bgcolor:颜色的情节paper_bgcolor颜色情节所在的地方

Question: Does Plotly have individual functions to update xaxis and yaxis?Answer: It indeed does, update_xaxes and update_yaxes has attributes like showgrid, title_font, etc which can be modified as per the requirements.

问题:Plotly是否有单独的功能来更新xaxis和yaxis? 答:的确如此, update_xaxes和update_yaxes具有诸如showgrid,title_font等属性,可以根据要求进行修改。

Question: Can we give names to each subplot?Answers: Each subplot can be given a title by the attribute subplot_titles present in the make_subplots() function.

问题:我们可以给每个子图命名吗? 答案:每个子图都可以通过make_subplots()函数中存在的属性subplot_titles来获得标题。

I have also used the other functionalities like a Range Slider and buttons in Plotly which I will be discussing in my next article very soon!

我还使用了其他功能,例如范围滑块和 Plotly中的按钮 ,这些功能我将在下一篇文章中很快讨论!

This was the first time I touched upon Data Visualization using Python. I have also written few other articles on Data Viz using tools like PowerBI which can be found here

这是我第一次使用Python进行数据可视化。 我还使用PowerBI之类的工具在Data Viz上写了其他文章,可以在这里找到

I have also developed an alerting system app using PowerApps and written an article about an awesome function I used there to integrate it with my PowerBI reports. Do check that out too here.

我还使用PowerApps开发了一个警报系统应用程序,并写了一篇文章,介绍了我在其中使用过的强大功能将其与PowerBI报告集成在一起。 这里也要检查一下

Let me know if you have any queries or suggestions regarding this by commenting below or reach me out on Twitter for any fun discussions revolving around Data Viz or Pandas! :)

如果您对此有任何疑问或建议,请在下面评论中告诉我,或者在Twitter上与我联系,以获取有关Data Viz或Pandas的有趣讨论! :)

翻译自: https://medium.com/analytics-vidhya/basic-thoughts-while-working-with-plotly-for-python-3721d160303c

plotly python

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值