在python中以绘图方式创建子图 [英] creating subplots in python plotly

查看:134
本文介绍了在python中以绘图方式创建子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python和ploty还是比较陌生(目前少于3个月的实际编码). 我试图在情节中创建子图.我已经使用下面的代码(下面也附有代码段)以图形方式创建了图形,但是我似乎无法使用子图来使它们工作

I am fairly new to python and ploty (<3 months of actual coding at this point). I am trying to create subplots in plotly. I have created graphs in plotly using the code below(code snippets attached below as well), but I can't seem to get them to work using subplots

我创建了许多类型的数据图,在加强代码方面的任何帮助将不胜感激.

I create many of these types of data graphs and any help in tightening my code would be much appreciated.

这是一个示例数据集(以下是我知道如何在此站点上进行排队的唯一方法??)[注意:它不是我用来生成以下图形的完全相同的数据集,但足够接近了]:

Here is a sample dataset (below is the only way I know how to line it up on this site??) [NOTE: it is not the exact same dataset I used to generate the graphs below, but close enough]:

subjid_raw  studyarm    visit             pn_chg     fx_chg   totw_chg

20001       B           02_ASCRN              -1       -28      -30
20001       B           02_DAY001              0         0        0
20001       B           02_DAY029WK4         -12       -56      -76
20001       B           02_DAY092WK13        -17       -61      -88
20001       B           02_DAY183WK26        -19       -64      -93
20001       B           02_DAY274WK39        -13       -45      -65
20001       B           02_ZEOS              -22       -70     -102
20001       B           MTH06                -22       -74     -108
20001       B           MTH12                -23       -74     -109
20005       C           02_ASCRN               3         8       12
20005       C           02_DAY001              0         0        0
20005       C           02_DAY029WK4         -20       -80     -112
20005       C           02_DAY092WK13        -16       -68      -95
20005       C           02_DAY183WK26        -22       -69      -99
20005       C           02_DAY274WK39        -19       -71     -103
20005       C           02_ZEOS               -4       -26      -36
20005       C           MTH06                -17       -76     -105
20005       C           MTH12                -22       -72     -106
20007       D           02_ASCRN             -13         2      -12
20007       D           02_DAY001              0         0        0
20007       D           02_DAY029WK4           6       -15      -45
20007       D           02_DAY092WK13        -10       -26      -39
20007       D           02_DAY183WK26        -19       -72      -97
20007       D           02_DAY274WK39         -4       -30      -35
20007       D           02_ZEOS              -25       -71     -103
20007       D           MTH12                -24       -85     -117
20010       A           02_ASCRN              -5        -2       -6
20010       A           02_DAY001              0         0        0
20010       A           02_DAY029WK4         -24       -75     -102
20010       A           02_DAY092WK13         -3         1       -1
20010       A           02_DAY183WK26         -2         7        6
20010       A           02_DAY274WK39          1         9       13
20010       A           02_ZEOS                -6       -1       -7

需要: :我希望将子图显示为具有两行两列的子图(尽管由于只有三个,所以我不确定该图是否可行)要绘制图形的数据列??

NEED: I would like to have the subplots show as a subplot with two rows and two columns (although I am not sure that will work given there are only three columns of data to graph??)

import pandas as pd ##(version: 0.22.0)
import numpy as np ##(version: 1.14.0)

import plotly.graph_objs as go
import plotly.tools as tls
from plotly.offline import *    
import cufflinks as cf ##(version: 0.12.1)

init_notebook_mode(connected=True)
cf.go_offline()



dummy_data = pd.read_csv("desktop\dummy_data.csv")

a = dummy_data.groupby(['studyarm', 'visit']) ['fx_chg'].mean().unstack('studyarm').drop(['02_UNSCH','ZEOS'])
b = dummy_data.groupby(['studyarm', 'visit']) ['pn_chg'].mean().unstack('studyarm').drop(['02_UNSCH','ZEOS'])
c = dummy_data.groupby(['studyarm', 'visit']) ['totw_chg'].mean().unstack('studyarm').drop(['02_UNSCH','ZEOS'])

fig3 = a.iplot(kind='line', yTitle='Score', title='Dummy Data1', mode=markers, asFigure=True)
fig3['data'][0]['marker']['symbol'] = 'hexagram-open-dot'
fig3['data'][1]['marker']['symbol'] = 'circle-dot'
fig3['data'][2]['marker']['symbol'] = 'star-open-dot'
fig3['data'][3]['marker']['symbol'] = 'square'
iplot(fig3, filename='simple-plot')

fig4 = b.iplot(kind='line', yTitle='Score', title='Dummy Data2', mode=markers, asFigure=True)
fig4['data'][0]['marker']['symbol'] = 'hexagram-open-dot'
fig4['data'][1]['marker']['symbol'] = 'circle-dot'
fig4['data'][2]['marker']['symbol'] = 'star-open-dot'
fig4['data'][3]['marker']['symbol'] = 'square'
iplot(fig4, filename='simple-plot')

fig5 = c.iplot(kind='line', yTitle='Score', title='Dummy Data3', mode=markers, asFigure=True)
fig5['data'][0]['marker']['symbol'] = 'hexagram-open-dot'
fig5['data'][1]['marker']['symbol'] = 'circle-dot'
fig5['data'][2]['marker']['symbol'] = 'star-open-dot'
fig5['data'][3]['marker']['symbol'] = 'square'
iplot(fig5, filename='simple-plot')

## I have tried all forms of 
##fig = tls.make_subplots(rows=n, cols=n) ##but it just shows graphs as blank?  

在正确的方向上提供任何帮助或帮助都将不胜感激.

Any help or nudge in the right direction would be appreciated.

<iframe width="900" height="800" frameborder="0" scrolling="no" src="//plot.ly/~t3c/29.embed"></iframe>

<iframe width="900" height="800" frameborder="0" scrolling="no" src="//plot.ly/~t3c/31.embed"></iframe>

<iframe width="900" height="800" frameborder="0" scrolling="no" src="//plot.ly/~t3c/34.embed"></iframe>

推荐答案

您可以只使用袖扣的subplot函数.据我所知,没有任何文档.

You could just use cufflinks' subplot function. As far as I know there is no documentation for it.

cf.subplots([fig3, fig4, fig5],
             shape=(2,2)).iplot()

将给您:

这篇关于在python中以绘图方式创建子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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