0

我正在使用以下命令来创建小提琴图:

    fig = FF.create_violin(data, data_header='Number', group_header='Group', height=500, width=800, title= "Number of Videos Watched",colors='YlOrRd')
    py.iplot(fig, filename= "Number of Videos Watched")

我想使用我选择的两种颜色。我似乎无法弄清楚如何做到这一点。有人可以告诉我该怎么做吗?谢谢!!!

4

1 回答 1

0

It's a bit tricky but this example gives a good starting point. The data for the two plots is stored in a Pandas dataframe.

First lets create a list of random values

y = numpy.random.randn(Nr)

and random selections from those numbers

gr = numpy.random.choice(list(violins), Nr)

then the numbers are modified, each selection differently.

for i, letter in enumerate(violins):
    y[gr == letter] *= norm_params[i][1] + norm_params[i][0]

and assigned to a DataFrame

df = pd.DataFrame(dict(Score = y, Group = gr))

and finally the DataFrame is plotted

fig = plotly.tools.FigureFactory.create_violin(df, data_header='Score', group_header='Group', colors='YlOrRd')

If you replace the data with your own values, e.g. values1 and values2

#get the groups
gr = ['Value1' for i in range(Nr)]
gr.extend(['Value2' for i in range(Nr)])

#assign the values
y = numpy.random.randn(2*Nr)
y[0:Nr] = values1
y[Nr:] = values2

you could get your own violin plots.


Here is a minimal example which creates two violin plots.

enter image description here

import plotly
import numpy
import pandas as pd
plotly.offline.init_notebook_mode()
Nr = 250
violins = 'AB'
y = numpy.random.randn(Nr)
gr = numpy.random.choice(list(violins), Nr)
norm_params = [(0, 1.2), (0.7, 1)]

for i, letter in enumerate(violins):
    y[gr == letter] *= norm_params[i][1] + norm_params[i][0]
df = pd.DataFrame(dict(Score = y, Group = gr))

data_header = 'Score'
group_header = 'Group'

fig = plotly.tools.FigureFactory.create_violin(df, data_header='Score', group_header='Group', colors='YlOrRd')

plotly.offline.iplot(fig, filename='Two Violin Plots')
于 2017-02-15T22:05:44.770 回答