Esempio n. 1
0
def test_irregular_subplots():
    df = cf.datagen.bubble(10, 50, mode='stocks')
    figs = cf.figures(df, [
        dict(kind='histogram', keys='x', color='blue'),
        dict(kind='scatter', mode='markers', x='x', y='y', size=5),
        dict(
            kind='scatter', mode='markers', x='x', y='y', size=5, color='teal')
    ],
                      asList=True)
    figs.append(
        cf.datagen.lines(1).figure(bestfit=False,
                                   colors=['blue'],
                                   bestfit_colors=['pink']))
    base_layout = cf.tools.get_base_layout(figs)
    sp = cf.subplots(
        figs,
        shape=(3, 2),
        base_layout=base_layout,
        vertical_spacing=.15,
        horizontal_spacing=.03,
        specs=[[{
            'rowspan': 2
        }, {}], [None, {}], [{
            'colspan': 2
        }, None]],
        subplot_titles=['Histogram', 'Scatter 1', 'Scatter 2', 'Bestfit Line'])
    sp['layout'].update(showlegend=False)
    return sp
Esempio n. 2
0
def test_irregular_subplots():
	df = cf.datagen.bubble(10, 50, mode='stocks')
	figs = cf.figures(df, [
		dict(kind='histogram', keys='x', color='blue'),
		dict(kind='scatter', mode='markers', x='x', y='y', size=5),
		dict(kind='scatter', mode='markers', x='x', y='y',
			 size=5, color='teal')],asList=True)
	figs.append(cf.datagen.lines(1).figure(bestfit=False, colors=['blue'],
										   bestfit_colors=['pink']))
	base_layout = cf.tools.get_base_layout(figs)
	sp = cf.subplots(figs, shape=(3, 2), base_layout=base_layout,
					 vertical_spacing=.15, horizontal_spacing=.03,
					 specs=[[{'rowspan': 2}, {}], [None, {}],
							[{'colspan': 2}, None]],
					 subplot_titles=['Histogram', 'Scatter 1',
									 'Scatter 2', 'Bestfit Line'])
	sp['layout'].update(showlegend=False)
	return sp
Esempio n. 3
0
def test_irregular_subplots():
    df = cf.datagen.bubble(10, 50, mode="stocks")
    figs = cf.figures(
        df,
        [
            dict(kind="histogram", keys="x", color="blue"),
            dict(kind="scatter", mode="markers", x="x", y="y", size=5),
            dict(kind="scatter", mode="markers", x="x", y="y", size=5, color="teal"),
        ],
        asList=True,
    )
    figs.append(cf.datagen.lines(1).figure(bestfit=False, colors=["blue"], bestfit_colors=["pink"]))
    base_layout = cf.tools.get_base_layout(figs)
    sp = cf.subplots(
        figs,
        shape=(3, 2),
        base_layout=base_layout,
        vertical_spacing=0.15,
        horizontal_spacing=0.03,
        specs=[[{"rowspan": 2}, {}], [None, {}], [{"colspan": 2}, None]],
        subplot_titles=["Histogram", "Scatter 1", "Scatter 2", "Bestfit Line"],
    )
    sp["layout"].update(showlegend=False)
    return sp
Esempio n. 4
0
    def plot_chart(self, data_frame, style, chart_type):

        mode = 'line'

        if style is None: style = Style()

        marker_size = 1

        x = ''
        y = ''
        z = ''
        fig = None

        scale = 1

        try:
            if (style.plotly_plot_mode == 'offline_html'):
                scale = 2 / 3
        except:
            pass

        # check other plots implemented by Cufflinks
        if fig is None:

            cm = ColorMaster()

            # create figure
            data_frame_list = self.split_data_frame_to_list(data_frame, style)
            fig_list = []
            cols = []

            for data_frame in data_frame_list:
                cols.append(data_frame.columns)

            cols = list(np.array(cols).flat)

            # get all the correct colors (and construct gradients if necessary eg. from 'Blues')
            # need to change to strings for cufflinks

            color_list = cm.create_color_list(style, [], cols=cols)
            color_spec = []

            # if no colors are specified then just use our default color set from chart constants
            if color_list == [None] * len(color_list):
                color_spec = [None] * len(color_list)

                for i in range(0, len(color_list)):
                    # get the color
                    if color_spec[i] is None:
                        color_spec[i] = self.get_color_list(i)

                    try:
                        color_spec[i] = matplotlib.colors.rgb2hex(
                            color_spec[i])
                    except:
                        pass

            else:
                # otherwise assume all the colors are rgba
                for color in color_list:
                    color = 'rgba' + str(color)
                    color_spec.append(color)

            start = 0

            for i in range(0, len(data_frame_list)):
                data_frame = data_frame_list[i]

                if isinstance(chart_type, list):
                    chart_type_ord = chart_type[i]
                else:
                    chart_type_ord = chart_type

                end = start + len(data_frame.columns)
                color_spec1 = color_spec[start:start + end]
                start = end

                if chart_type_ord == 'surface':
                    fig = data_frame.iplot(
                        kind=chart_type,
                        title=style.title,
                        xTitle=style.x_title,
                        yTitle=style.y_title,
                        x=x,
                        y=y,
                        z=z,
                        mode=mode,
                        size=marker_size,
                        theme=style.plotly_theme,
                        bestfit=style.line_of_best_fit,
                        legend=style.display_legend,
                        colorscale=style.color,
                        dimensions=(style.width * style.scale_factor * scale,
                                    style.height * style.scale_factor * scale),
                        asFigure=True)

                elif chart_type_ord == 'line':
                    chart_type_ord = 'scatter'
                elif chart_type_ord == 'scatter':
                    mode = 'markers'
                    marker_size = 5
                elif chart_type_ord == 'bubble':
                    x = data_frame.columns[0]
                    y = data_frame.columns[1]
                    z = data_frame.columns[2]

                # special case for choropleth which has yet to be implemented in Cufflinks
                # will likely remove this in the future
                elif chart_type_ord == 'choropleth':

                    for col in data_frame.columns:
                        try:
                            data_frame[col] = data_frame[col].astype(str)
                        except:
                            pass

                    if style.color != []:
                        color = style.color
                    else:
                        color = [[0.0, 'rgb(242,240,247)'], [0.2, 'rgb(218,218,235)'], [0.4, 'rgb(188,189,220)'], \
                                 [0.6, 'rgb(158,154,200)'], [0.8, 'rgb(117,107,177)'], [1.0, 'rgb(84,39,143)']]

                    text = ''

                    if 'text' in data_frame.columns:
                        text = data_frame['Text']

                    data = [
                        dict(type='choropleth',
                             colorscale=color,
                             autocolorscale=False,
                             locations=data_frame['Code'],
                             z=data_frame[
                                 style.plotly_choropleth_field].astype(float),
                             locationmode=style.plotly_location_mode,
                             text=text,
                             marker=dict(
                                 line=dict(color='rgb(255,255,255)', width=1)),
                             colorbar=dict(title=style.units))
                    ]

                    layout = dict(
                        title=style.title,
                        geo=dict(
                            scope=style.plotly_scope,
                            projection=dict(type=style.plotly_projection),
                            showlakes=True,
                            lakecolor='rgb(255, 255, 255)',
                        ),
                    )

                    fig = dict(data=data, layout=layout)

                if chart_type_ord not in ['surface', 'choropleth']:

                    fig = data_frame.iplot(
                        kind=chart_type_ord,
                        title=style.title,
                        xTitle=style.x_title,
                        yTitle=style.y_title,
                        x=x,
                        y=y,
                        z=z,
                        subplots=False,
                        mode=mode,
                        size=marker_size,
                        theme=style.plotly_theme,
                        bestfit=style.line_of_best_fit,
                        legend=style.display_legend,
                        color=color_spec1,
                        dimensions=(style.width * style.scale_factor * scale,
                                    style.height * style.scale_factor * scale),
                        asFigure=True)

                fig.update(dict(layout=dict(legend=dict(x=0.05, y=1))))

                fig.update(dict(layout=dict(paper_bgcolor='rgba(0,0,0,0)')))
                fig.update(dict(layout=dict(plot_bgcolor='rgba(0,0,0,0)')))

                fig_list.append(fig)

            if len(fig_list) > 1:
                import cufflinks
                fig = cufflinks.subplots(fig_list)
            else:
                fig = fig_list[0]

        self.publish_plot(fig, style)
#!/usr/bin/python3
import pandas as pd
import numpy as np
from optparse import OptionParser
import pathos.multiprocessing as mp
import cufflinks
import plotly as py
from tqdm import tqdm
import time

pd.set_option('expand_frame_repr', False)

df=pd.read_csv('del.csv',index_col=[0])
pvt=df.pivot_table(values='mse',columns='method', index=['quant_size','number_of_bins','std_threshold'],aggfunc=[lambda x: x.count()/len(x),np.mean]).rename(columns={'<lambda>':'pass','mean':'mse'})
reshape=x=pvt.stack().reset_index(drop=False).pivot_table(index=['pass'],values=['mse'],columns=['method','number_of_bins'])
if 0:
    reshape.columns=reshape.columns.swaplevel(0,2)
    py.offline.plot(reshape[(23,'mse_from_modulo_method')].dropna().sort_index().iplot(asFigure=True))
else:
    py.offline.plot(reshape.mse.mse_from_modulo_method.dropna().sort_index().iplot(asFigure=True))
exit()
columns=[i[:-1] for i in reshape.columns]
f2=cufflinks.subplots([reshape[i].figure() for i in columns],subplot_titles=[str(i) for i in columns])
print('hi')
py.offline.plot(f2)
Esempio n. 6
0
df.iplot(subplots=True, subplot_titles=True, legend=False)

df = cf.datagen.bubble(10, 50, mode='stocks')
figs = cf.figures(df, [
    dict(kind='histogram', keys='x', color='blue'),
    dict(kind='scatter', mode='markers', x='x', y='y', size=5),
    dict(kind='scatter', mode='markers', x='x', y='y', size=5, color='teal')
],
                  asList=True)
figs.append(
    cf.datagen.lines(1).figure(bestfit=True,
                               colors=['blue'],
                               bestfit_colors=['pink']))
base_layout = cf.tools.get_base_layout(figs)
sp = cf.subplots(
    figs,
    shape=(3, 2),
    base_layout=base_layout,
    vertical_spacing=.15,
    horizontal_spacing=.03,
    specs=[[{
        'rowspan': 2
    }, {}], [None, {}], [{
        'colspan': 2
    }, None]],
    subplot_titles=['Histogram', 'Scatter 1', 'Scatter 2', 'Bestfit Line'])
sp['layout'].update(showlegend=False)
cf.iplot(sp)

help(df.iplot)
                  title='up视频质量走势参考',
                  xTitle='时间',
                  yTitle='比率')
        return df3.figure(
            kind='lines',
            orientation='v',
        )

    def month_video_play(self):
        df1 = self.df_data[[
            'created', 'stat#favorite', 'stat#coin', 'stat#like'
        ]]
        df2 = df1.groupby([df1.created.dt.year,
                           df1.created.dt.month]).agg('sum')
        # df2.iplot(kind='bar', barmode='stack', orientation='v')
        df2.iplot(kind='histogram', bins=10)
        return df2


if __name__ == '__main__':
    upid = 176037767
    analysis_video = AnalysisVideo(upid)
    figs = cf.figures(analysis_video.month_video_play(),
                      [dict(kind='bar', barmode='stack')],
                      asList=True)
    figs.append(analysis_video.month_video_play_rate())
    layout = cf.tools.get_base_layout(figs)
    sp = cf.subplots(figs, shape=(1, 2), base_layout=layout)
    sp['layout'].update(showlegend=False)
    cf.iplot(sp)
# In[9]:


train[['Sex', 'Survived']].groupby('Sex').mean().iplot(kind='bar', colors='blue', title='Survival Rate based on Gender')


# Around 74% females in the sample survived compared to 18% males.
# Our 1st assumption seems to hold true.

# In[10]:


survived = train[train['Survived'] == 1]['Age']
not_survived = train[train['Survived'] == 0]['Age']

cf.subplots([survived.figure(kind='hist', colors='blue'), not_survived.figure(kind='hist', colors='blue')],
            shared_yaxes=True, subplot_titles=['Survived', 'Not Survived']).iplot()


# The histogram above shows us that survival rate of children is more compared to adults. <br>
# Few more observations:
# * Infant (Age < 5) survival rate is more <br>
# * Most of the passengers aged between 15 to 35 did not survived <br>
# * Very few of the oldest passengers (Age=80) survived

# In[11]:


train[['Pclass', 'Survived']].groupby('Pclass').mean().iplot(kind='bar', colors='blue', title='Survival Rate based on Passenger Class')


# Class 1 passengers clearly had the highest survival rate followed by Class 2 and 3 
 def show(self):
     self.figure = cf.Figure(
         cf.subplots(self.figure_list, base_layout=self.figure_list[1].to_dict()['layout'], shared_xaxes=True, shape=(len(self.figure_list), 1)))
     self.figure.show()