本文介绍了Plotly:将线添加到条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自数据框的绘图条形图:
I have a plotly bar chart, from a dataframe:
fig = df.iplot(asFigure=True, kind='bar', barmode = 'relative')
py.iplot(fig)
是否可以将数据框中的一列变成一行?
Is it possible to turn one of the columns in the data frame into a line series?
推荐答案
评论中的建议链接确实有一些有价值的资源,但他们不会直接回答您的问题.iplot()
使用 pandas 数据框作为输入,并生成堆叠条形图.这是一种可以让您完全做到这一点的方法,尽管不使用 df.iplot()
The suggested link in the comments does have some valuable resources, but they won't answer your questions directly. iplot()
uses a pandas dataframe as input, and produces a stacked barplot. Here's an approach that will let you do exactly that, albeit without using df.iplot()
一、剧情:
现在,代码
go.Bar(x=df['x'],
y=df['y4'])
go.Scatter(x=df['x'],
y=df['y4'])
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import pandas as pd
import numpy as np
N = 20
x = np.linspace(1, 10, N)
y = np.random.randn(N)+3
y2 = np.random.randn(N)+6
y3 = np.random.randn(N)+9
y4 = np.random.randn(N)+12
df = pd.DataFrame({'x': x, 'y': y, 'y2':y2, 'y3':y3, 'y4':y4})
df.head()
data = [
go.Bar(
x=df['x'], # assign x as the dataframe column 'x'
y=df['y']
),
go.Bar(
x=df['x'],
y=df['y2']
),
go.Bar(
x=df['x'],
y=df['y3']
),
go.Scatter(
x=df['x'],
y=df['y4']
)
]
layout = go.Layout(
barmode='stack',
title='Stacked Bar with Pandas'
)
fig = go.Figure(data=data, layout=layout)
# IPython notebook
iplot(fig, filename='pandas-bar-chart-layout')
这篇关于Plotly:将线添加到条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!