Plotly:如何使用 plotly 作为 Pandas 的绘图后端来制作不同的图? [英] Plotly: How to make different plots using plotly as a plotting backend for pandas?

查看:89
本文介绍了Plotly:如何使用 plotly 作为 Pandas 的绘图后端来制作不同的图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 plotly 设置为 pandas 的后端,您可以使用以下方法快速轻松地生成绘图:

Setting plotly as a backend for pandas, you can produce plots quickly and easily using:

df.plot()

如何使此设置生成线图以外的其他图?还有哪些其他选择?

How can you make this setup produce other plots than lineplots? And what other options are there?

推荐答案

您可以通过以下中的 kind 参数定义您想要生成的绘图类型:

You can define which plot type you'd like to produce through the kind argument in:

df.plot(kind='line')

kind='line' 生成与问题中完全相同的图.有效选项是:

kind='line' produces the very same plot as in the question. 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()

情节:

请注意,我使用了 kinds[:-1].这是因为 imshow 用于图像数据并且需要一个比 [1,2,3,4,5,6]bit 的数据集>.请参考 this使用来自熊猫数据帧的 imshow 绘制图像

Notice that I've used 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]. Please refer to this and Plotting an image with imshow from a pandas dataframe

为了设置所有其他选项的标题和标题颜色,这里有一个完整的代码片段:

In order to set the titles and title colors for all other options, here's a complete code snippet:

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()

这篇关于Plotly:如何使用 plotly 作为 Pandas 的绘图后端来制作不同的图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆