简体   繁体   English

Plotly:如何使用 plotly 作为 pandas 的绘图后端来制作不同的绘图?

[英]Plotly: How to make different plots using plotly as a plotting backend for pandas?

Setting plotly as a backend for pandas, you can produce plots quickly and easily using:将 plotly 设置为 pandas 的后端,您可以使用以下方法快速轻松地生成绘图:

df.plot()

在此处输入图像描述

How can you make this setup produce other plots than lineplots?您如何使此设置产生除线图以外的其他图? And what other options are there?还有哪些其他选择?

You can define which plot type you'd like to produce through the kind argument in:您可以通过以下中的kind参数定义要生成的 plot 类型:

df.plot(kind='line')

kind='line' produces the very same plot as in the question. kind='line'产生与问题相同的 plot 。 Valid options are:有效的选项是:

['scatter', 'line', 'area', 'bar',
 'barh', 'hist', 'box', 'violin',
 'strip', 'funnel', 'density_heatmap',
 'density_contour', 'imshow']

You can easily study them all by running:您可以通过运行轻松地研究它们:

for k in kinds[:-1]:
    df.plot(kind=k).show()

Plots:情节:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Notice that I've used kinds[:-1] .请注意,我使用了kinds[:-1] This is because imshow is for image data and requires a dataset a bit more complicated than [1,2,3,4,5,6] .这是因为imshow用于图像数据,并且需要[1,2,3,4,5,6]更复杂的数据集。 Please refer to this and Plotting an image with imshow from a pandas dataframe请参考内容并从 pandas dataframe 中使用 imshow 绘制图像

In order to set the titles and title colors for all other options, here's a complete code snippet:为了为所有其他选项设置标题和标题 colors,这里有一个完整的代码片段:

import random
import pandas as pd

random.seed(123)
df = pd.DataFrame({'x':[1,2,3,4,5,6]})
pd.options.plotting.backend = "plotly"

kinds = ['scatter', 'line', 'area', 'bar', 'barh', 'hist', 'box', 'violin', 'strip', 'funnel', 'density_heatmap', 'density_contour', 'imshow']

for k in kinds[:-1]:
    fig = df.plot(kind=k, title = k)
    fig.update_layout(title = dict(font=dict(color='#EF553B')))
    fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM