def gantt_chart_between_date(filename, data_1, data_2):

    #dict_from_dates = from_csv_to_gant_chartt(filename)

    #dict_in= dict_from_csv_in(filename)

    dict_out = dict_from_csv_out(filename)

    date_time_obj_1 = datetime.datetime.strptime(data_1, '%Y-%m-%d')

    date_time_obj_2 = datetime.datetime.strptime(data_2, '%Y-%m-%d')

    for key, value in dict_out.items():

        for i in range(len(value)):

            if value[i][0] >= date_time_obj_1 and value[i][
                    1] <= date_time_obj_2:

                if key not in dict_to_plot:

                    dict_to_plot[key] = [[
                        value[i][0], value[i][1], value[i][2], value[i][3]
                    ]]

                else:

                    dict_to_plot[key].append(
                        [value[i][0], value[i][1], value[i][2], value[i][3]])

    plt.figure(num=1, figsize=[20, 40], dpi=100)

    plt.xlabel("Date", weight="bold")

    plt.ylabel("IP", weight="bold")

    plt.ylim(0, len(dict_to_plot.keys()))

    # number of keys in the dict
    nrow = len(dict_to_plot.keys())

    y_pos = np.arange(nrow)

    width = 0.6

    plt.tick_params(axis='y', labelsize=8)

    plt.yticks(y_pos, reversed(dict_to_plot.keys()))

    # plot the gantt chart from the dict of elements

    for key, value in dict_to_plot.items():

        nrow = nrow - 1

        for i in range(len(value)):

            plt.broken_barh([(value[i][0], value[i][2])], (nrow, width))

    plt.show()
Exemple #2
0
def gantetu(name, df):
    plt.figure(num=name + ' 甘特图', figsize=(8, 5))
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.ylim((0, 5))
    plt.xticks([x for x in range(28)])
    plt.xlabel('time')
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    #判断是否选中过颜色
    is_select = [False for _ in range(10)]
    for i in range(len(df)):
        pcolor = int(re.search('\d+', df.iloc[i]['进程']).group())
        if is_select[pcolor] == False:
            plt.broken_barh(
                [(int(df.iloc[i]['到达时间']), int(df.iloc[i]['服务时间']))], [0, 1],
                facecolors=color[pcolor],
                label='进程 ' + df.iloc[i]['进程'])
            plt.text(df.iloc[i]['到达时间'], 1.1, df.iloc[i]['进程'])
        else:
            plt.broken_barh(
                [(int(df.iloc[i]['到达时间']), int(df.iloc[i]['服务时间']))], [0, 1],
                facecolors=color[pcolor])
            plt.text(df.iloc[i]['到达时间'], 1.1, df.iloc[i]['进程'])
        is_select[pcolor] = True
    plt.legend()
    plt.show()
Exemple #3
0
def plotComp(segments, title, tileSize, chromDict, figExt):
	plt.figure()
	yIndex = 0.1
	yHeight = 0.8
	sortedChroms = sorted(chromDict.keys())
	labels, inds, cinds = makeLabels()
	OT = open("composition_%s.tab"%(title), 'w')
	OT.write("Chr\t"+'\t \t'.join(labels)+'\t \tChr Length\n')
	for chrom in sortedChroms:
		otStr = '%s\t'%(chrom)
		chromSize = chromDict[chrom]
		X = np.zeros(2**len(nameList)-1)
		for arrayStr, size in segments[chrom]:
			sortedInd = inds[int(arrayStr,2)-1]
			X[sortedInd] += size*tileSize
		percents = list(np.round(X/float(chromSize),3))
		sP = map(lambda x: str(x*100)+'%', percents)
		otStr += '\t'.join([str(val) for tup in zip(X,sP) for val in tup])+'\t'+str(chromSize)+'\n'
		OT.write(otStr)
		xranges = zip(np.cumsum([0]+percents[:-1]), percents)
		plt.broken_barh(xranges, (yIndex, yHeight), lw=0, color=[colors[i] for i in cinds])
		yIndex += 1
	OT.close()
	plt.xlim((0,1))
	plt.yticks(np.arange(0.5, len(sortedChroms)), sortedChroms)
	plt.ylabel("Chromosome")
	plt.xlabel("Fraction of Chromosome")
	plt.title(title+" Chromosome Composition")
	patches = [mpatches.Patch(color=colors[cinds[i]], label=labels[i]) for i in xrange(len(labels))]
	plt.figlegend(patches, labels, loc='center right', ncol=1, frameon=False)
	plt.tight_layout(rect=[0,0,0.81,1.0])
	plt.savefig("composition_%s.%s"%(title, figExt))
	plt.close()
    def plot_cumulative(self, X, names=None):
        """ Plot cumulative SWEAT associations for target terms X
        """
        deltas = [[
            np.mean([m.wv.similarity(w, a) for a in self.A]) -
            np.mean([m.wv.similarity(w, b) for b in self.B]) for w in X
        ] for m in [self.model_1, self.model_2]]

        title = None
        attr_labels = [self.names["A"], self.names["B"]]
        mod_labels = [self.names["X1"], self.names["X2"]]
        bar_cols = ['#e15759', '#4e79a7']
        dot_cols = ['black', 'white']

        if names is not None:
            if "Title" in names.keys(): title = names['Title']
            if "Attributes" in names.keys(): attr_labels = names['Attributes']
            if "Models" in names.keys(): mod_labels = names["Models"]
            if "Bar Colors" in names.keys(): bar_cols = names["Bar Colors"]
            if "Dot Colors" in names.keys(): dot_cols = names["Dot Colors"]

        plt.figure(figsize=(7, 2))

        xl = 0

        for i, s_mod in enumerate(deltas):

            cumulative = sum(s_mod)

            pos = sum([x for x in s_mod if x > 0])
            neg = sum([x for x in s_mod if x < 0])

            plt.broken_barh([(0, pos)],
                            yrange=(i - 0.4, 0.8),
                            facecolors=(bar_cols[0]),
                            label=attr_labels[0])
            plt.broken_barh([(neg, abs(neg))],
                            yrange=(i - 0.4, 0.8),
                            facecolors=(bar_cols[1]),
                            label=attr_labels[1])

            plt.scatter([cumulative], [i],
                        facecolor=dot_cols[0],
                        edgecolor=dot_cols[1],
                        label='cumulate')

            xl = max(xl, max(abs(pos), abs(neg)))

        xl += xl / 10
        plt.xlim(-xl, xl)

        handles, labels = plt.gca().get_legend_handles_labels()

        plt.legend(handles=handles[:3], labels=labels[:3])
        plt.axvline(0, lw=1, color='k', alpha=0.5)
        plt.xlabel("Cumulative association")
        plt.yticks(range(len(deltas)), mod_labels)
        if title is not None:
            plt.title(title)
        plt.show()
def create_precip_plot(storm_arr):
    # Plotting precipitation distribution time series

    # Creating a new figure instance
    plt.figure(1)

    # Labeling the x and y axes
    plt.xlabel("Time (years)", fontsize=14)
    plt.ylabel("Rainfall Intensity (mm/day)", fontsize=14)

    # Setting the plot title
    plt.title("Randomly Generated Rainfall Time Series", fontsize=16)

    # Now to plot the axes the way we'd like them...
    ax = plt.gca()

    # Manually set the ten times (in hours) to plot across 100 years
    tick_locations = [
        0,
        3652.42,
        7304.84,
        10957.26,
        14609.68,
        18262.1,
        21914.52,
        25566.96,
        29219.36,
        32871.78,
        36524.2,
    ]

    # This next list will actually replace the hours with time in years.
    tick_labels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

    # Swapping out the two tick labels to plot intensity against years.
    plt.xticks(tick_locations, tick_labels)

    # Setting tick label size.
    ax.tick_params(labelsize=14)

    # Setting the limits for the x and y
    plt.xlim(0, 36524.2)
    plt.ylim(ymin=0, ymax=20)

    # Looping through the storm array to plot the intensity as the height of each bar plot
    # and the width will correspond to the storm duration.

    for s in storm_arr:
        x = storm_arr.index(s)
        start = storm_arr[x][0]
        end = storm_arr[x][1] - storm_arr[x][0]
        plt.broken_barh(
            [(start, end)], (0, storm_arr[x][2]), label="Rain", color="blue"
        )

    plt.show()
Exemple #6
0
def create_precip_plot(storm_arr):
    # Plotting precipitation distribution time series

    # Creating a new figure instance
    plt.figure(1)

    # Labeling the x and y axes
    plt.xlabel("Time (years)", fontsize=14)
    plt.ylabel("Rainfall Intensity (mm/day)", fontsize=14)

    # Setting the plot title
    plt.title("Randomly Generated Rainfall Time Series", fontsize=16)

    # Now to plot the axes the way we'd like them...
    ax = plt.gca()

    # Manually set the ten times (in hours) to plot across 100 years
    tick_locations = [
        0,
        3652.42,
        7304.84,
        10957.26,
        14609.68,
        18262.1,
        21914.52,
        25566.96,
        29219.36,
        32871.78,
        36524.2,
    ]

    # This next list will actually replace the hours with time in years.
    tick_labels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

    # Swapping out the two tick labels to plot intensity against years.
    plt.xticks(tick_locations, tick_labels)

    # Setting tick label size.
    ax.tick_params(labelsize=14)

    # Setting the limits for the x and y
    plt.xlim(0, 36524.2)
    plt.ylim(ymin=0, ymax=20)

    # Looping through the storm array to plot the intensity as the height of each bar plot
    # and the width will correspond to the storm duration.

    for s in storm_arr:
        x = storm_arr.index(s)
        start = storm_arr[x][0]
        end = storm_arr[x][1] - storm_arr[x][0]
        plt.broken_barh([(start, end)], (0, storm_arr[x][2]),
                        label="Rain",
                        color="blue")

    plt.show()
def createMultiUserTemporalClusterEvolution(
        userlist,
        df,
        n_clusters,
        cluster_col,
        title="Temporal User Cluster Evolution"):
    '''
    Plots temporal evolution of a list of users belonging to a dataframe
    processed on a clustering algorithm for a given number of clusters.

    "df": dataframe containing the data cols (user, date, pred_clusters) we want a temporal plot.
    "userlist": list containing the names of the different users from a dataframe we want to plot.
    "n_clusters": number of distinct clusters.
    "cluster_col": name of the column storing the predicted clusters.

    Note: max n_clusters: 9
    Note: date column name is default to be named as "date_time".
    '''
    fig, ax = plt.subplots(figsize=(6, 3))
    y_ticks_arr = list()
    clusters_list = list()
    for n_cluster in range(n_clusters):
        clusters_list.append("Cluster " + str(n_cluster))

    color_list = [
        "green", "orange", "red", "cyan", "blue", "yellow", "magenta", "black",
        "pink"
    ]
    for index, user_name in enumerate(userlist):
        user_dataframe = df.where(df["user"] == user_name).dropna()
        user_series = pd.Series(user_dataframe[cluster_col].values,
                                index=user_dataframe["date_time"])

        for n_cluster in range(n_clusters):
            s_cluster = user_series[user_series == n_cluster]
            inxval = matplotlib.dates.date2num(s_cluster.index.to_pydatetime())
            times = zip(inxval, np.ones(len(s_cluster)))
            plt.broken_barh(list(times), (index, 1),
                            color=color_list[n_cluster])

        y_ticks_arr.append(index)
        index += 2

    ax.margins(0)
    ax.set_yticks(y_ticks_arr)
    #ax.set_yticks([])
    ax.set_yticklabels(userlist)
    ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())
    ax.xaxis.set_minor_locator(matplotlib.dates.DayLocator())
    ax.legend(clusters_list, loc="upper left")
    ax.set_title(title)
    monthFmt = matplotlib.dates.DateFormatter("%b")
    ax.xaxis.set_major_formatter(monthFmt)
    plt.tight_layout()
    plt.show()
def plot_locations(locations: List[int],
                   locations_names: List[str],
                   timestamps: List[datetime.datetime]):
    for location, timestamp in zip(locations, timestamps):
        l_name = locations_names[location]
        color = LOCATION_TO_COLOR[l_name.split(":")[0]]
        plt.broken_barh([(timestamp, 1 / 24)], (location, 1), facecolor=color, label=l_name)
    plt.title("Location")
    plt.ylim((0, len(locations_names)))
    plt.xlabel("Time")
    plt.gcf().autofmt_xdate()
Exemple #9
0
def plot_worker_activity(data, pdf, title=None):
    workers = sorted(data.keys())

    width = .8
    padding = .2

    baseline = padding / 2

    fig = plt.figure()
    ax = fig.add_subplot(111)

    for worker in workers:
        last_started = None
        last_running = None
        last_blocked = None
        active_ranges = []
        running_ranges = []
        for (timestamp, (task_id, status)) in data[worker]:
            if status == 'initialized':
                last_started = timestamp
                last_running = None
                last_blocked = None
            if status == 'freed':
                active_ranges.append((last_started, timestamp - last_started))
                last_started = None
            if status == 'running':
                if last_blocked == timestamp:
                    (last_running, _) = running_ranges.pop()
                else:
                    last_running = timestamp
            if status == 'blocked':
                last_blocked = timestamp
                running_ranges.append((last_running, timestamp - last_running))

        plt.broken_barh(active_ranges, (baseline, width), color='gray')
        plt.broken_barh(running_ranges, (baseline, width),
                        color='orange',
                        linewidth=0.0)

        baseline += width + padding

    ax.set_ylabel('Node ID and Worker ID')
    ax.set_yticks(list(0.5 + x for x in range(len(workers))))
    ax.set_yticklabels(map(lambda x: str(x), workers))
    ax.set_xlabel('Time [seconds]')

    if title is not None:
        ax.set_title(title)
    else:
        ax.set_title('Worker Activity')

    pdf.savefig(fig)
    plt.close(fig)
Exemple #10
0
def plot_mask_multiline(mask, columns=500, facecolors='blue'):
    '''Plots a sequence of boolean values spanning multiple lines.

    The contiguous True values are aggregated into blocks and the False values
    are not plotted.'''

    lines_of_intervals = (intervals_where(mask[x:x + columns])
                          for x in range(0, len(mask), columns))

    # lines as sequences of (xmin, xwidth)
    lines_of_xranges = [[(start, end - start) for start, end in line]
                        for line in lines_of_intervals]

    plt.axis([0, columns, len(lines_of_xranges), 0])
    for offset, xranges in enumerate(lines_of_xranges):
        plt.broken_barh(xranges, (offset, 0.8), facecolors=facecolors)
Exemple #11
0
def plot_simstat(simstat):
    plt.figure()
    i = 0
    start = simstat["StartUTC"].min()
    cmap = plt.cm.get_cmap("hsv", 10)
    stations = []
    for stat, group in simstat.groupby(["StationGroup", "StationNum"]):
        x = [((s - start).total_seconds() / 60, (e - s).total_seconds() / 60)
             for (s, e) in zip(group["StartUTC"], group["EndUTC"])]
        plt.broken_barh(xranges=x,
                        yrange=(i * 10, 5),
                        label=stat,
                        color=cmap(i))
        stations.append(stat)
        i += 1
    plt.gca().legend(ncol=len(stat), bbox_to_anchor=(0, 1), loc="lower left")
    plt.show()
Exemple #12
0
def graph_broken_barh(key,graph_dict, comparison):
    plt.broken_barh(graph_dict[key]['ibd'] , (key-0.25, 0.5),label='IBD', facecolors='orange')
    plt.broken_barh(graph_dict[key]['dbd'] , (key-0.25, .5),label='DBD', facecolors = 'blue')
    
    
    plt.legend(loc='right')
    plt.ylim(0,15)
    plt.yticks(np.arange(15))
    plt.xlabel('Position')
    plt.ylabel('Chromosome')
    plt.xscale('linear')
    plt.title(comparison)
    plt.tick_params(axis='x',          # changes apply to the x-axis
                    which='both',      # both major and minor ticks are affected
                    bottom='off',      # ticks along the bottom edge are off
                    top='off',         # ticks along the top edge are off
                    labelbottom='off')
Exemple #13
0
def tornado_diagramm(names, lows, values, base):
    # The actual drawing part

    # The y position for each variable
    ys = range(len(values))[::-1]  # top to bottom

    # Plot the bars, one by one
    for y, low, value in zip(ys, lows, values):
        # The width of the 'low' and 'high' pieces
        low_width = base - low
        high_width = low + value - base

        # Each bar is a "broken" horizontal bar chart
        plt.broken_barh(
            [(low, low_width), (base, high_width)],
            (y - 0.4, 0.8),
            facecolors=['white', 'white'],  # Try different colors if you like
            edgecolors=['black', 'black'],
            linewidth=1,
        )

        # Display the value as text. It should be positioned in the center of
        # the 'high' bar, except if there isn't any room there, then it should be
        # next to bar instead.
        x = base + high_width / 2
        if x <= base + 50:
            x = base + high_width + 50
        plt.text(x, y, str(value), va='center', ha='center')

    # Draw a vertical line down the middle
    plt.axvline(base, color='black')

    # Position the x-axis on the top, hide all the other spines (=axis lines)
    axes = plt.gca()  # (gca = get current axes)
    axes.spines['left'].set_visible(False)
    axes.spines['right'].set_visible(False)
    axes.spines['bottom'].set_visible(False)
    axes.xaxis.set_ticks_position('top')

    # Make the y-axis display the variables
    plt.yticks(ys, variables)

    # Set the portion of the x- and y-axes to show
    plt.xlim(base - 1000, base + 1000)
    plt.ylim(-1, len(variables))
Exemple #14
0
    def plot_location_plots_with_zones(ltuo_model_id_and_hashtag_tag, no_of_bins_for_influence_score=100):
        output_file_format = fld_results+'/%s_%s.png'
        for model_id, hashtag_tag in ltuo_model_id_and_hashtag_tag:
            no_of_zones, ltuo_location_and_influence_score_and_zone_id = \
                Experiments.get_location_with_zone_ids(model_id, hashtag_tag)
            locations, influence_scores, zone_ids = zip(*ltuo_location_and_influence_score_and_zone_id)
#            print len(locations)
#            print [zone_id for _, _, zone_id in sorted(zip(locations, influence_scores, zone_ids), key=itemgetter(1))]
#            exit()
            # Plot influence plot
            ltuo_location_and_global_influence_score = zip(locations, influence_scores)
            max_y_tick = InfluenceAnalysis._plot_scores(ltuo_location_and_global_influence_score, [], no_of_bins_for_influence_score, smooth=True)
            # Plot zones
            ltuo_influence_score_and_zone_id = zip(influence_scores, zone_ids)
            ltuo_zone_id_and_influence_scores = [(zone_id, zip(*ito_tuo_influence_score_and_zone_id)[0])
                                                    for zone_id, ito_tuo_influence_score_and_zone_id in
                                                        groupby(
                                                                sorted(ltuo_influence_score_and_zone_id, key=itemgetter(1)),
                                                                key=itemgetter(1)
                                                        )
                                                ]
            ltuo_zone_id_and_tuo_min_influence_score_and_max_influence_score = \
                [(zone_id, (min(influence_scores), max(influence_scores))) for zone_id, influence_scores in ltuo_zone_id_and_influence_scores]
            ltuo_zone_id_and_tuo_box_start_and_box_width = \
                [(zone_id, (min_influence_score, abs(min_influence_score-max_influence_score))) 
                     for zone_id, (min_influence_score, max_influence_score) in 
                        ltuo_zone_id_and_tuo_min_influence_score_and_max_influence_score
                ]
            zone_ids, ltuo_box_start_and_box_width = zip(*ltuo_zone_id_and_tuo_box_start_and_box_width)
            zone_colors = [GeneralMethods.getRandomColor() for zone_id in zone_ids]
            plt.broken_barh(ltuo_box_start_and_box_width , (0, max_y_tick), facecolors=zone_colors, alpha=0.25, lw=0)
#            temp_ltuo_box_start_and_box_width = []
#            for box_start, box_width in ltuo_box_start_and_box_width:
#                if box_width!=0: temp_ltuo_box_start_and_box_width.append((box_start, box_width))
#                else: temp_ltuo_box_start_and_box_width.append((box_start, 0.0001))

#            zero_size_cluster_ltuo_box_start_and_box_width = []
#            for box_start, box_width in ltuo_box_start_and_box_width:
#                if box_width==0: zero_size_cluster_ltuo_box_start_and_box_width.append((box_start, 0.0001))
#            plt.broken_barh(zero_size_cluster_ltuo_box_start_and_box_width , (0, max_y_tick), facecolors='r', alpha=0.25, lw=0)
#            plt.xlim(xmin=-0.0025, xmax=0.0025)
            output_file = output_file_format%(GeneralMethods.get_method_id(), model_id, hashtag_tag)
            savefig(output_file)
Exemple #15
0
 def showPlot(self, X, Y, u, v, p, obstacle, titleText="no text"):
     # Plot the last figure on screen
     fig = plt.figure(figsize=(100, 50), dpi=25)
     plt.contourf(X, Y, p, alpha=0.5)  # alpha - background intensity
     plt.tick_params(axis='both', which='major', labelsize=80)
     cbar = plt.colorbar()
     cbar.ax.tick_params(labelsize=80)
     plt.contour(X, Y, p)
     M = np.hypot(u, v)
     plt.quiver(X, Y, u, v, M, scale=1 / 0.02)  ##plotting velocity
     # plt.scatter(X, Y, color='r')
     plt.broken_barh([(obstacle[0], obstacle[1])],
                     (obstacle[2], obstacle[3]),
                     facecolors='grey',
                     alpha=0.8)
     plt.xlabel('X')
     plt.ylabel('Y')
     plt.title(titleText, fontsize=80)
     plt.show()
def plot_encounters(encounters: Dict[str, List[int]],
                    risky_encounters: Dict[str, List[int]],
                    contamination_encounters: Dict[str, List[int]],
                    timestamps: List[datetime.datetime]):
    risky_encounters_human_ids = set()
    contamination_encounters_human_ids = set()
    for other_human in encounters:
        other_human_id = int(other_human[6:])
        for encounter, timestamp in zip(encounters[other_human], timestamps):
            if not encounter:
                continue
            safe_color = ENCOUNTER_TO_COLOR["non-infectuous"]
            plt.broken_barh([(timestamp, 1 / 24)], (other_human_id - 1, 1), color=safe_color, label=other_human)
        for encounter, timestamp in zip(risky_encounters[other_human], timestamps):
            if not encounter:
                continue
            risky_encounters_human_ids.add(other_human_id)
            risky_color = ENCOUNTER_TO_COLOR["infectuous"]
            plt.broken_barh([(timestamp, 1 / 21)], (other_human_id - 1, 1), color=risky_color, label=other_human)
        for encounter, timestamp in zip(contamination_encounters[other_human], timestamps):
            if not encounter:
                continue
            contamination_encounters_human_ids.add(other_human_id)
            contamination_color = ENCOUNTER_TO_COLOR["contamination"]
            plt.broken_barh([(timestamp, 1 / 12)], (other_human_id - 1, 1), color=contamination_color,
                            label=other_human)

    plt.title("Encounters")
    plt.ylim((0, len(encounters)))
    plt.yticks([int(tick / 4 * len(encounters)) for tick in range(4 + 1)] +
               list(risky_encounters_human_ids) +
               list(contamination_encounters_human_ids))
    # plt.set_yticklabels(locations_names)
    plt.xlabel("Time")
    plt.gcf().autofmt_xdate()
Exemple #17
0
def create_thresh_plot(fire_arr,storm_arr, exceed_arr, max_intense=1000):

    # This function takes the different arrays of fire, storm and threshold-
    # exceeding events and plots them to show potentially erosion-inducing storms.

    plt.figure(1)
    plt.xlabel('Time (years)', fontsize=16) ## x axis label
    plt.ylabel('Rainfall Intensity (mm/day)', fontsize=16) ## y axis label
    plt.title('Randomly Generated Rainfall Time Series', fontsize=18) ## chart title
    ax = plt.gca()
    tick_locations=[0, 3652.42, 7304.84, 10957.26, 14609.68, 18262.1, 21914.52, 25566.96, 29219.36, 32871.78, 36524.2]#labels = range(ticks.size)
    tick_labels=[0,10,20,30,40,50,60,70,80,90,100]
    plt.xticks(tick_locations, tick_labels)
    ax.tick_params(labelsize=16)
    plt.ylim(ymin=0, ymax=max_intense)
    plt.xlim(0, 36524.2)
    for f in fire_arr:
        y = fire_arr.index(f)
        start = fire_arr[y][0]
        end = fire_arr[y][0] + 1.0
        plt.broken_barh([(start, 1)], ((max_intense-200),max_intense), label='Fire', color='orange')

    for s in storm_arr:
        x = storm_arr.index(s) ##creates rainfall graph. x=length of storm. y=intensity
        start = storm_arr[x][0]
        end = storm_arr[x][1] - storm_arr[x][0]
        plt.broken_barh([(start, end)], (0,storm_arr[x][2]), label='Rain', color = 'blue') ## for each Tr period

    for t in exceed_arr:
        z = exceed_arr.index(t)
        start = exceed_arr[z][0]
        end = exceed_arr[z][1] - exceed_arr[z][0]
        plt.broken_barh([(start, end)], (0,exceed_arr[z][2]), label='Threshold Exceeded', color='red')
    plt.show()
Exemple #18
0
    def plot(self, bar_height_fraction=0.9):
        '''Plots the timeline as a horizontal bar chart,
with unfinished events as triangles to indicate the start.'''
        # only import matplotlib if we actually need it, so we don't use it during flight
        global plt
        from matplotlib import pyplot as plt

        colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
        plt.xlabel('Elapsed time [s]')
        plt.yticks(ticks=range(1, 2 * len(self.data.keys()) + 1, 2),
                   labels=self.data.keys())
        for i, dataset in enumerate(self.finished_data):
            color = colors[i % len(colors)]
            plt.scatter(self.unfinished_data[dataset],
                        [2 * i + 1] * len(self.unfinished_data[dataset]),
                        marker='>',
                        c=color)
            plt.broken_barh(
                self.finished_data[dataset],
                (2 * i + (1 - bar_height_fraction), bar_height_fraction * 2),
                facecolor=color)
        plt.show()
    def plot_result(self, save_dir):
        '''
        入力がクラス系列のみ(ex: 444111...)対応,文字は×
        '''
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)

        # HSMM&GP-HSMMのクラス系列の取得
        for num_sequence, sequence in enumerate(self.segm_sentences):
            hsmm_class = []
            # 下位のクラス系列の取得
            class_sequence = ''.join(sequence)
            gphsmm_class_str = list(class_sequence)
            gphsmm_class = [int(gp_cls) for gp_cls in gphsmm_class_str]
            print(gphsmm_class)
            print(len(gphsmm_class))
            # この階層のHSMMのクラス系列の取得
            for segm in sequence:
                # print(self.word_class[id(segm)])
                segm_class = self.word_class[id(segm)]
                hsmm_class.extend([segm_class] * len(segm))
            print(hsmm_class)
            print(len(hsmm_class))

            # 上下でプロット
            class_state = np.arange(self.num_class)
            hsmm_class = np.array(hsmm_class)
            hsmm_class_x = np.arange(len(hsmm_class))
            hsmm_range = np.c_[hsmm_class_x[:-1], np.diff(hsmm_class_x)]
            colors = ['r', 'b', 'g']

            plt.figure()
            plt.subplot(211)
            plt.plot(range(len(gphsmm_class)), gphsmm_class)
            plt.subplot(212)
            for i, color in zip(class_state, colors):
                plt.broken_barh(hsmm_range[hsmm_class[:-1] == i, :], (0, 1),
                                facecolors=color)
            plt.savefig(save_dir + 'class_segm%03d.png' % num_sequence)
Exemple #20
0
    def plot_save(self, y_data, x_data=None, f_name="_", phase_data=None,
                  use_legend=True, title=""):
        plt.gca().set_ylim(-1.15, 1.1)
        phases = []
        if phase_data is not None:
            for j in range(len(phase_data)):
                phase = phase_data[j]
                if not phase in phases:
                    plt.broken_barh([(j, 1)], (-0.5, 1),
                                    facecolors=self.color_dict[phase],
                                    label=phase)
                    phases.append(phase)
                else:
                    plt.broken_barh([(j, 1)], (-0.5, 1),
                                    facecolors=self.color_dict[phase])
            if use_legend is True:
                plt.legend()

        if isinstance(y_data[0], list) is False:
            y_data = [y_data]
        if x_data is None:
            x_data = range(1, max([len(y) for y in y_data]) + 4)
        for j, y in enumerate(y_data):
            if f_name.split('/')[0] in ["kb_smoothed_curves"]:
                if j < 2:
                    plt.plot(x_data[:len(y)], y, color=["cyan", "red", "black",
                                                        "orangered"][j],
                             )
                else:
                    plt.plot(x_data[:len(y)], y, dashes=[2, 1],
                                color="darkblue")

            else:
                plt.plot(x_data[:len(y)], y, color=["cyan", "red", "black",
                                                     "orangered"][j])
        if title != "":
            plt.title(title)
        plt.gcf().savefig(f"{self.dir_}/{f_name}.png", dpi=300)
        plt.gcf().clear()
def plot_bars(ys, lows, values, base):
    for y, low, value in zip(ys, lows, values):
        # The width of the 'low' and 'high' pieces
        low_width = (base - low)
        high_width = (low + value - base)

        # Each bar is a "broken" horizontal bar chart
        plt.broken_barh(
            [(low, low_width), (base, high_width)],
            (y - 0.4, 0.8),
            facecolors=['blue', 'blue'],  # Try different colors if you like
            edgecolors=['black', 'black'],
            linewidth=1,
        )

        # Display the value as text. It should be positioned in the center of
        # the 'high' bar, except if there isn't any room there, then it should be
        # next to bar instead.
        x = base + high_width / 2
        if x <= base + 15:
            x = base + high_width + 7
        plt.text(x, y, str(value), va='center', ha='center')
def plot_graphical_representation(task_dict):
    """
    Plots sequence of tasks
    """
    Y_MAX = 10
    Y_MIN = 0
    Y_MID = (Y_MAX - Y_MIN) / 2

    fig = plt.figure()
    for taskId, execution_time_list in task_dict.items():
        for start_time, end_time in execution_time_list:
            duration = end_time - start_time
            plt.broken_barh([(start_time, duration)], (Y_MIN, Y_MAX),
                            figure=fig,
                            label=taskId)
            midpoint = start_time + (duration / 2)
            plt.text(midpoint,
                     Y_MID,
                     taskId,
                     figure=fig,
                     color="black",
                     fontsize="x-large",
                     ha="center")
    plt.show()
def plot_ethogram(etho, name, offset=0, legend=True):
    global event_color_scheme
    ranges = get_ethogram_ranges(etho, 1 / 120.0)
    for ethoname, ethorange, ethocolor in zip(ranges[0], ranges[1], ranges[2]):
        if ethoname == 'earFlick':
            plt.vlines(ethorange[0],
                       offset,
                       offset + 1,
                       linewidth=2,
                       linestyles='dotted')
        else:
            plt.broken_barh([ethorange], [offset, 1],
                            label=name,
                            color=[ethocolor],
                            linewidth=0)
    patches = [
        mpatch.Rectangle((0, 0), 1, 1, fc=color)
        for color in event_color_scheme.itervalues()
    ]
    if legend:
        plt.legend(patches, [c for c in event_color_scheme.iterkeys()],
                   bbox_to_anchor=(1.05, 1),
                   loc=2,
                   borderaxespad=0.)
def plot_timeline(userid, posdata, plot_distance = False):
	pos = cont_position(posdata) #transform into continius position
	colorkeys = [lat+lon for lat, lon, confidence in pos if confidence > 0.0 ]
	confidences = [confidence for lat, lon, confidence in pos if confidence > 0.0 ]

	cmap = plt.get_cmap("Paired")
	if plot_distance:
		colorkeys = [vincenty((pos[i][0],pos[i][1]), (pos[i+1][0],pos[i+1][1])).kilometers if pos[i+1][2] > 0.0 else 0 for i in range(len(pos)-1) if pos[i][2] > 0.0] + [0.0]
		cmap = plt.get_cmap("Reds")

	norm = mpl.colors.Normalize()
	sm = mpl.cm.ScalarMappable(norm=norm, cmap=cmap)

	fig = plt.figure(1)
	fig.set_figheight(3)
	startdates = mins2datenum(range(24*60))
	enddates = mins2datenum(range(1,24*60+1))
	xranges = [(s,e-s) for s,e in zip(startdates, enddates)]
	xranges = [xr for xr, p in zip(xranges,pos) if p[2] > 0.0]
	colors = [(r,g,b,a) for (r,g,b,a), confidence in zip(sm.to_rgba(colorkeys), confidences)]

	assert(len(xranges) == len(colors))

	plt.broken_barh([xr for xr, c in zip(xranges,confidences) if c > 0.0], (0,0.5), facecolors = [col for col, c in zip(colors, confidences) if c > 0.0], linewidth = 0.0)
	plt.broken_barh([xr for xr, c in zip(xranges,confidences) if c > 0.2], (0,1), facecolors = [col for col, c in zip(colors, confidences) if c > 0.2], linewidth = 0.0)
	plt.ylim(0,1)
	plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
	plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=2))
	plt.xlim((mdates.date2num(config.SAMPLE_DAY_NO_TZ), mdates.date2num(config.SAMPLE_DAY_NO_TZ + datetime.timedelta(days=1))))
	plt.gcf().autofmt_xdate()

	if plot_distance:
		plt.savefig("figs/dist/" + userid + ".png")
	else:
		plt.savefig("figs/timelines/" + userid + ".png")
	plt.close()	
Exemple #25
0
def create_thresh_plot(fire_arr, storm_arr, exceed_arr, max_intense=1000):

    # This function takes the different arrays of fire, storm and threshold-
    # exceeding events and plots them to show potentially erosion-inducing storms.

    plt.figure(1)
    plt.xlabel('Time (years)', fontsize=16)  ## x axis label
    plt.ylabel('Rainfall Intensity (mm/day)', fontsize=16)  ## y axis label
    plt.title('Randomly Generated Rainfall Time Series',
              fontsize=18)  ## chart title
    ax = plt.gca()
    tick_locations = [
        0, 3652.42, 7304.84, 10957.26, 14609.68, 18262.1, 21914.52, 25566.96,
        29219.36, 32871.78, 36524.2
    ]  #labels = range(ticks.size)
    tick_labels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    plt.xticks(tick_locations, tick_labels)
    ax.tick_params(labelsize=16)
    plt.ylim(ymin=0, ymax=max_intense)
    plt.xlim(0, 36524.2)
    for f in fire_arr:
        y = fire_arr.index(f)
        start = fire_arr[y][0]
        end = fire_arr[y][0] + 1.0
        plt.broken_barh([(start, 1)], ((max_intense - 200), max_intense),
                        label='Fire',
                        color='orange')

    for s in storm_arr:
        x = storm_arr.index(
            s)  ##creates rainfall graph. x=length of storm. y=intensity
        start = storm_arr[x][0]
        end = storm_arr[x][1] - storm_arr[x][0]
        plt.broken_barh([(start, end)], (0, storm_arr[x][2]),
                        label='Rain',
                        color='blue')  ## for each Tr period

    for t in exceed_arr:
        z = exceed_arr.index(t)
        start = exceed_arr[z][0]
        end = exceed_arr[z][1] - exceed_arr[z][0]
        plt.broken_barh([(start, end)], (0, exceed_arr[z][2]),
                        label='Threshold Exceeded',
                        color='red')
    plt.show()
Exemple #26
0
def plot_centers(ind_greedy, N, alg, **kwargs):
    """
    Plot centers chosen by greedy algorithms
    """

    if 'num_centers' in kwargs:
        x_pos = np.sort(ind_greedy[:kwargs['num_centers'] + 1])
    else:
        x_pos = np.sort(ind_greedy)

    if 'init' in kwargs:
        x_pos += kwargs['init']
    xran = list(zip(x_pos[:], 1.0 * np.ones(len(x_pos[:]))))

    if alg == 'f':
        plt.broken_barh(xran, (0, 1), facecolors=('green'))
    elif alg == 'p':
        plt.broken_barh(xran, (0, 1), facecolors=('blue'))
    elif alg == 'psr':
        plt.broken_barh(xran, (0, 1), facecolors=('magenta'))

    # ax1 = ax #fig.gca()
    plt.xlim([0, N + 1])

    plt.yticks([])
    if 'times_offline' in kwargs:
        try:
            dtTmp = np.round((kwargs['times_offline'][1:] -
                              kwargs['times_offline'][:-1]).mean())
            plt.xticks(
                np.linspace(kwargs['init'], N, 5),
                np.round(
                    np.linspace(dtTmp * kwargs['init'],
                                kwargs['times_offline'][N], 5) / 3600.))
        except:
            plt.xticks(
                np.linspace(0, N, 5),
                np.round(
                    np.linspace(0, kwargs['times_offline'][N], 5) / 3600.))
    else:
        plt.xticks([])
Exemple #27
0
# Labelling tickes of y-axis 
gnt.set_yticklabels(np.asarray(Gates, dtype="<U16", order='C')) 
  
# Setting graph attribute 
gnt.grid(True) 

# PLOT SCHEDULE
for i in range(len(solution_df)):

    # Set color based on capacity of aircraft
    if Flights_class[solution_df['flight_number'][i] - 1] == 1:
        color = 'Red'
        label = 'Small aircraft'
    
    elif Flights_class[solution_df['flight_number'][i] - 1] == 2:
        color = 'Blue'
        label = 'Medium aircraft'
        
    elif Flights_class[solution_df['flight_number'][i] - 1] == 3:
        color = 'Orange'
        label = 'Large aircraft'
    
    plt.broken_barh([(solution_df['start'][i] + 0.03, solution_df['end'][i] - solution_df['start'][i] - 0.03,)], (solution_df['gate_number'][i] - 0.45, 0.9), color=color, label=label)

plt.savefig('figures/schedule_scen1')
print("15. Plots available \n")
print("====================================================================================== ")
print("----------------------------Execution Completed--------------------------------------- ")
print("====================================================================================== \n ")
Exemple #28
0
def broken_barh(*args, **kwargs):
    r"""starkplot wrapper for broken_barh"""
    return _pyplot.broken_barh(*args, **kwargs)
Exemple #29
0
def add_bar_plot(x,y):
    """
    Plot a single box plot of y values at coordinate x

    :param y: list of numbers
    :param x: x coord
    :return:
    """

    # percentiles of interest
    perc = [min(y), scoreatpercentile(y,10), scoreatpercentile(y,25),
                   scoreatpercentile(y,50), scoreatpercentile(y,75),
                   scoreatpercentile(y,90), max(y)]
    midpoint = x # time-series time

    # min/max
    plt.broken_barh([(midpoint-.01,.02)], (perc[0], perc[1]-perc[0]),edgecolor="k",facecolor="w")
    plt.broken_barh([(midpoint-.01,.02)], (perc[5], perc[6]-perc[5]),edgecolor="k",facecolor="w")
    # 10/90
    plt.broken_barh([(midpoint-.1,.2)], (perc[1], perc[2]-perc[1]),edgecolor="r",facecolor="w")
    plt.broken_barh([(midpoint-.1,.2)], (perc[4], perc[5]-perc[4]),edgecolor="r",facecolor="w")
    # 25/75
    plt.broken_barh([(midpoint-.4,.8)], (perc[2], perc[3]-perc[2]),edgecolor="b",facecolor="w")
    plt.broken_barh([(midpoint-.4,.8)], (perc[3], perc[4]-perc[3]),edgecolor="c",facecolor="w")
Exemple #30
0
def make_tornado_plot(dataframe, factors, show=False, fig_path=None):
    dfs = os.listdir(dataframe)
    print 'pickled dfs: {}'.format(dfs)
    filename = 'norm_sensitivity.pkl'
    if filename in dfs:

        df = read_pickle(os.path.join(dataframe, filename))
        df.to_csv(os.path.join(fig_path, 'sample_norm_df.csv'))
        print df
        xx = 1

        for index, row in df.iterrows():
            print index, row
            base = row[0][5]
            lows = []
            for fact in row:
                lows.append(min(fact))
            lows = array(lows)
            values = []
            for fact in row:
                values.append(max(fact))

            # The y position for each variable
            ys = range(len(values))[::-1]  # top to bottom

            # Plot the bars, one by one
            for y, low, value in zip(ys, lows, values):
                # The width of the 'low' and 'high' pieces
                low_width = base - low
                high_width = abs(value - base)
                # Each bar is a "broken" horizontal bar chart
                plt.broken_barh(
                    [(low, low_width), (base, high_width)],
                    (y - 0.4, 0.8),
                    facecolors=['white', 'white'],  # Try different colors if you like
                    edgecolors=['black', 'black'],
                    linewidth=1)
                plt.subplots_adjust(left=0.32)

                # Display the value as text. It should be positioned in the center of
                # the 'high' bar, except if there isn't any room there, then it should be
                # next to bar instead.
                x = base + high_width / 2
                if x <= base:
                    x = base + high_width
                    # plt.text(x, y, str(round(value - low, 1)) + 'mm', va='center', ha='center')

            # Draw a vertical line down the middle
            plt.axvline(base, color='black')

            # Position the x-axis on the top, hide all the other spines (=axis lines)
            axes = plt.gca()  # (gca = get current axes)
            axes.spines['left'].set_visible(False)
            axes.spines['right'].set_visible(False)
            axes.spines['bottom'].set_visible(False)
            axes.xaxis.set_ticks_position('top')

            # Make the y-axis display the factors
            plt.yticks(ys, factors)
            print 'location: {}'.format(index)
            plt.title('{} [mm]'.format(index.replace('_', ' ')),
                      y=1.05)
            # Set the portion of the x- and y-axes to show
            plt.xlim(min(-20, 1.2 * min(lows)), base + 1.1 * max(values))
            plt.ylim(-1, len(factors))
            # plt.show()
            if show:
                plt.show()
            # if fig_path:
            #     plt.savefig('{}_tornado'.format(index), fig_path, ext='jpg', dpi=500, close=True, verbose=True)
            plt.close()
    def region_plot(
        self,
        chr,
        posbeg,
        posend,
        savefigname="trial.png",
        percentile=2,
        plot_snp_list=None,
        gene_file_name="/Volumes/samaras/Data/TAIR9/TAIR9_GFF3_genes.gff",
    ):
        """
        OBSELETE 
        gwa plot, regional
        percentile : the top #% to be included in the final plot
        full_plot : toggles whether the entire span of 5 chromosomes are plotted regardless of what is inside the data
        """
        import matplotlib

        matplotlib.use("Agg")
        import matplotlib.pyplot as plt

        self.find_threshold(percentile)

        # initializing plot
        plt.figure(figsize=(12, 7.5))
        plt.axes([0, 0.25, 1, 0.75])

        # plotting data points
        sys_write("Plotting data points... ")
        for data_indicator in xrange(1, self.num_dif_results + 1):
            scores = []
            positions = []
            for res in self.res:
                if res.chr != chr:
                    continue
                if res.pos > posend or res.pos < posbeg:
                    continue
                if res.indicator == data_indicator:  # This is so not optimal!
                    if res.score >= self.thresholds[data_indicator - 1]:
                        scores.append(res.score)
                        positions.append(res.plotpos)
            plt.plot(positions, scores, ".", markersize=3, alpha=0.7)
        sys_write("Done.\n")

        # plotting axis
        sys_write("Plotting axes and extras... ")
        # Creating a ticklist for the region; there should be separate scale lists
        ticklist = []
        ticklabels = []
        # dynamic labeling of intervals
        pos_range = posend - posbeg
        log10scale = int(math.log10(pos_range))
        log10residue = float(pos_range) / math.pow(10, log10scale)
        if log10residue < 1.2:
            log10multiplier = 0.2
        elif log10residue >= 1.2 and log10residue < 2.6:
            log10multiplier = 0.5
        elif log10residue >= 2.6 and log10residue < 5.8:
            log10multiplier = 1
        else:
            log10multiplier = 2
        scale_interval = int(log10multiplier * math.pow(10, log10scale))
        if scale_interval >= 1000000:
            label_scale_interval = scale_interval / 1000000
            label_scale_alphabet = "Mb"
        elif scale_interval >= 1000:
            label_scale_interval = scale_interval / 1000
            label_scale_alphabet = "kb"
        else:
            label_scale_interval = scale_interval
            label_scale_alphabet = "bp"
        for i in xrange(int((posbeg - 1) / scale_interval) + 1, int((posend) / scale_interval) + 1):
            ticklabels.append(i * label_scale_interval)
            ticklist.append(self.chr_offsets[chr - 1] + i * scale_interval)
        plt.axis(
            [
                self.chr_offsets[chr] + posbeg,
                self.chr_offsets[chr] + posend,
                self.min_score - self.plot_padding,
                self.max_score + self.plot_padding,
            ]
        )
        plt.xticks(ticklist, ticklabels)
        plt.ylabel("$-log(p-$value$)$", size="large")
        plt.xlabel(label_scale_alphabet, size="large")
        # Plots custom snp list
        if plot_snp_list != None:
            for custom_snp in plot_snp_list:
                if custom_snp.chr == chr:
                    if custom_snp.pos >= posbeg and custom_snp.pos <= posend:
                        plt.plot(
                            [custom_snp.plotpos, custom_snp.plotpos],
                            [self.min_score - self.plot_padding, self.max_score + self.plot_padding],
                            "b-",
                            linewidth=0.5,
                        )
        sys_write("Done\n")

        # plotting genomes
        if not self.G:
            import gene_info

            sys_write("Loading gene model information... ")
            self.G = gene_info.genes(gene_file_name)
            sys_write("Done.\n")

        l_genes = self.G.get_genes_in_range(chr, posbeg, posend)
        if len(l_genes) > 50:
            print "Skipping gene plots: too many genes in range: %s. " % len(l_genes)
        else:
            sys_write("Plotting gene models... ")
            plt.axes([0, 0, 1, 0.18])
            plt.axis([self.chr_offsets[chr - 1] + posbeg, self.chr_offsets[chr - 1] + posend, -3, 0])
            plt.axis("off")
            broken_barh_xranges = []
            broken_barh_yranges = (-0.5, 0.5)
            annotate_y = -1
            for gene in l_genes:
                broken_barh_xranges.append((self.chr_offsets[chr - 1] + gene.posbeg, gene.posend - gene.posbeg))
                plt.annotate(gene.id, (self.chr_offsets[chr - 1] + gene.posbeg, annotate_y), rotation=270, size="small")
            plt.broken_barh(broken_barh_xranges, broken_barh_yranges, facecolors="yellow", alpha=0.5)
            sys_write("Done.\n")

        # saving figure
        sys_write("Outputting figure to file... ")
        plt.savefig(savefigname, format="png", dpi=300, bbox_inches="tight")
        sys_write("Done.\n")
        start = pd.Timestamp(to_list(row['day_in_day_out'])[0], unit='s')
        finish = pd.Timestamp(to_list(row['day_in_day_out'])[1], unit='s')
        righe.append({'id + Ip': idip, 'start': start, 'finish': finish})

    data = pd.DataFrame(righe, columns=['id + Ip', 'start', 'finish'])
    print(data)

    data.sort_values("start", axis=0, ascending=False, inplace=True)

    data.reset_index(drop=True, inplace=True)

    data["delta"] = data["finish"] - data["start"]

    pprint(data["delta"])

    data["PastTime"] = data["start"] - data["start"][0]

    nrow = len(data)
    plt.figure(num=1, figsize=[8, 5], dpi=100)
    width = 0.6

    for i in range(nrow):
        ii = nrow - i - 1

        plt.broken_barh([(data["start"][ii], data["delta"][ii])],
                        (i - width / 2, width),
                        color="b")

    y_pos = np.arange(nrow)
    plt.yticks(y_pos, labels=reversed(data["id + Ip"]))
    #plt.show()
Exemple #33
0
def tornado_matplotlib(graph_data, base, directory, file_name, variable):
    """Creates a tornado diagram and saves it to a prespecified directory
       Inputs:
           graphs_data - a df which must contain the columns 'variables' for 
               the names of the variables being graphed, 'lower' for the lower
               bounds of the deterministic sensitivity and 'ranges' for the total
               range between the lower and upper
           base - a float with the base case value
           directory - str - a path to a directory where the plot should be saved
           file_name - str- a useful name by which the plot with be saved
       Exports:
           A chart to the prespecified directory
    """
    # Sort the graph data so the widest range is at the top and reindex
    graph_data.copy()
    graph_data = graph_data.sort_values('ranges', ascending=False)
    graph_data.index = list(range(len(graph_data.index)))[::-1]

    # The actual drawing part
    fig = plt.figure()

    # Plot the bars, one by one
    for index in graph_data.index:
        # The width of the 'low' and 'high' pieces

        # If to ensure visualisation is resilient to lower value of parameter
        # leading to higher estimate of variable
        if graph_data.loc[index, 'upper'] > graph_data.loc[index, 'lower']:
            low = graph_data.loc[index, 'lower']
            face_colours = ['red', 'green']
        else:
            low = graph_data.loc[index, 'upper']
            face_colours = ['green', 'red']
        value = graph_data.loc[index, 'ranges']
        low_width = base - low
        high_width = low + value - base

        # Each bar is a "broken" horizontal bar chart
        plt.broken_barh(
            [(low, low_width), (base, high_width)],
            (index - 0.4, 0.8),
            facecolors=face_colours,  # Try different colors if you like
            edgecolors=['black', 'black'],
            linewidth=1,
        )

    # Draw a vertical line down the middle
    plt.axvline(base, color='black', linestyle='dashed')

    # Position the x-axis and hide unnecessary axes
    ax = plt.gca()  # (gca = get current axes)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')

    # Set axis labels
    label = re.sub('_', ' ', variable).title()
    plt.xlabel(label)
    plt.ylabel('Model Parameters')

    # Make the y-axis display the variables
    plt.yticks(graph_data.index.tolist(), graph_data['variables'])

    # Set the portion of the x- and y-axes to show
    plt.xlim(left=0)
    plt.ylim(-1, len(graph_data.index))

    # Stop scientific formats
    ax.get_xaxis().get_major_formatter().set_scientific(False)
    ax.get_xaxis().set_major_formatter(
        plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
    fig.autofmt_xdate()

    # Change axis text format
    plt.rcParams.update({'font.size': 12})
    plt.rcParams.update({'font.family': 'calibri'})

    # Make it tight format
    plt.tight_layout()

    # Set up export path and export the chart
    path = os.path.join(directory, file_name + '.png')
    fig.savefig(path)
    print('Please find the chart at', path)
    plt.close(fig=None)
Exemple #34
0
      high = row["High result"]
    else:
      low = row["High result"]
      high = row["Low result"] 

    base = (low + high) / 2
    low_width = base - low
    high_width = high - base
    value = row["Variable"]
    y = ys[i]

    # Each bar is a "broken" horizontal bar chart
    plt.broken_barh(
        [(low, low_width), (base, high_width)],
        (y - 0.4, 0.8),
        facecolors=['#90C3D4', '#90C3D4'],  # Try different colors if you like
        edgecolors=['#90C3D4', '#90C3D4'],
        linewidth=1,
    )

    # Display the value as text. It should be positioned in the center of
    # the 'high' bar, except if there isn't any room there, then it should be
    # next to bar instead.
    x = base + high_width / 2
    if x <= base + 50:
        x = base + high_width + 50
    # plt.text(x, y, str(base), va='center', ha='left')

  # Draw a vertical line down the middle
  plt.axvline(real_base, color='black')
	observed = np.array(create_observation_sequence(hidden, B))
	print('Observed: ', observed)
	print('Hidden: ', hidden)

	# Tuples of form index value, number of continuous values corresponding to Fair State
	indices_hidden_fair = np.where(hidden==0)[0]
	tuples_contiguous_values_fair = list(group(indices_hidden_fair))
	tuples_start_break_fair = create_tuple(tuples_contiguous_values_fair)

	# Tuples of form index value, number of continuous values corresponding to Biased State
	indices_hidden_biased = np.where(hidden==1)[0]
	tuples_contiguous_values_biased = list(group(indices_hidden_biased))
	tuples_start_break_biased = create_tuple(tuples_contiguous_values_biased)

	# Tuples for observations
	observation_tuples=[]
	for i in range(6):
	    observation_tuples.append(create_tuple(group(list(np.where(observed==i)[0]))))

    # Make plots
	plt.subplot(2,1,1)
	plt.xlim((0, num_calls));
	plt.title('Observations');
	for i in range(6):
	    plt.broken_barh(observation_tuples[i],(i+0.5,1),facecolor='k');
	plt.subplot(2,1,2);
	plt.xlim((0, num_calls));
	plt.title('Hidden States Blue:Fair, Red: Biased');
	plt.broken_barh(tuples_start_break_fair,(0,1),facecolor='b');
	plt.broken_barh(tuples_start_break_biased,(0,1),facecolor='r');
	plt.savefig('hmm.png')
Exemple #36
0
def generate_addresses(fd,
                       names,
                       base,
                       low_res=False,
                       gen_mirror=False,
                       plot_map=False):
    '''
    Generate addresses with increasing bitwidth
    '''
    not_mirrored, mirrored = [], []
    mirror_base = -1
    register_names = sorted(names, key=lambda x: gch.get(x)[0], reverse=True)
    if gen_mirror:
        mirror_size = sum([
            1 << gch[k][0] for k in register_names
            if (1 << gch[k][0]) <= MIN_MIRROR_ARRAY_SIZE
        ])
    for k in register_names:
        bitwidth = gch[k][0]
        register_array_size = 1 << gch[k][0]
        if (gen_mirror and mirror_base == -1 and register_array_size <= MIN_MIRROR_ARRAY_SIZE):
            mirror_base = base
            mirror_bit_len = mirror_size.bit_length()
            mirror_size_nearest_pow2 = 1 << mirror_bit_len
            if mirror_base & (mirror_size_nearest_pow2 - 1) != 0:
                print(
                    'Mirror base NOT aligned.\nMirror Base: {};\nMirror Size: {};'.
                    format(format(base, '#x'), format(mirror_size, '#x')))
                mirror_base = ((mirror_base + mirror_size_nearest_pow2) >>
                               mirror_bit_len) << mirror_bit_len
                base = mirror_base
                print('Aligning mirror base. New mirror base: {}'.format(
                    format(base, '#x')))
            mirror_clk_prefix = 'lb'  # TODO: This is a hack
            if fd:
                s = '`define MIRROR_WIDTH %d\n'\
                    '`define ADDR_HIT_MIRROR (%s_addr[`LB_HI:`MIRROR_WIDTH]==%d)\n' %\
                    (mirror_bit_len, mirror_clk_prefix, mirror_base >> mirror_bit_len)
                fd.write(s)
        sign = gch[k][2]
        datawidth = gch[k][3]
        description = gch[k][-1]
        k_aw = 1 << bitwidth  # address width of register name k
        if (k_aw - 1) & base == 0:
            next_addr = base
        else:
            # A way to come up with the next available address
            next_addr = ((base + k_aw) >> bitwidth) << bitwidth
        if bitwidth > 0 and not low_res:
            for reg_index in range(k_aw):
                r_name = k + '_' + str(reg_index)
                add_to_global_map(r_name, next_addr + reg_index, sign,
                                  bitwidth, datawidth, description)
        else:
            add_to_global_map(k, next_addr, sign, bitwidth, datawidth,
                              description)
        if fd:
            s = '`define ADDR_HIT_%s (%s_addr%s[`LB_HI:%d]==%d) '\
                '// %s bitwidth: %d, base_addr: %d\n'
            fd.write(s %
                     (k, gch[k][4], gch[k][5], bitwidth, next_addr >> bitwidth,
                      gch[k][1], bitwidth, next_addr))
        (not_mirrored
         if mirror_base == -1 else mirrored).append((next_addr, k_aw))
        base = next_addr + k_aw
    if plot_map and (mirrored or not_mirrored):
        from matplotlib import pyplot as plt
        import matplotlib.ticker as ticker
        if not_mirrored:
            plt.broken_barh(not_mirrored, (0, 1))
        if mirrored:
            plt.broken_barh(mirrored, (0, 1), facecolors=('red'))
        axes = plt.gca()
        axes.get_xaxis().set_major_formatter(ticker.FormatStrFormatter("%#x"))
        plt.show()
    return base
Exemple #37
0
def showOneTask(num, data, tag):
    xdata = [(date2num(r[1]), date2num(r[2]) - date2num(r[1])) for r in data[["start", "end"]].itertuples()]
    plt.broken_barh(xdata, (num, 1), facecolors=next(cycol), label=tag)
Exemple #38
0
    v_new[-1, :] = 0
    v_new[:, 0] = 0
    v_new[:, -1] = 0

    u_new[x_obs:(x_obs + wdth),
          y_obs:(y_obs + dpth)] = 0  # velocity in the obstacle is 0
    v_new[x_obs:(x_obs + wdth), y_obs:(y_obs + dpth)] = 0

    # Assigning old new values to the old variables
    for i in range(x_points):
        for j in range(y_points):
            u[i, j] = u_new[i, j]
            v[i, j] = v_new[i, j]
            P[i, j] = P_new[i, j]

    if it % 100 == 0:
        print("Completed iteration: " + str(it))

print("\n Pressure distribution is: \n" + str(P))

# Plotting results
plt.contourf(y_gr, x_gr, P, cmap='cool', alpha=0.5)
plt.colorbar()
plt.quiver(y_gr, x_gr, u, v)  # Plot u and v as arrows
# https://pythontic.com/visualization/charts/brokenbarchart_horizontal - Plotting the obstacle
yrange = [(y_gr[y_obs], dpth * del_y)]
xrange = (x_gr[x_obs], wdth * del_x)
plt.broken_barh(yrange, xrange, facecolors='red', alpha=0.8)
plt.title("Pressure distribution at time-step :" + str(num_itrs))
plt.show()
def plot_efficiency(userid, posdata):
	pos = cont_position(posdata) #transform into continius position
	#calculate efficiency
	dist = [vincenty((pos[i][0],pos[i][1]),(pos[i+1][0],pos[i+1][1])).kilometers if pos[i][2] > 0.0 and pos[i+1][2] > 0.0 else 0.0 for i in range(0,len(pos)-1)] #distance in km if consecutive positions available
	efficiencies = []
	for t in range(24*60):
		window = 30 #minutes window left and right
		t1 = t - window
		t2 = t + window

		#search for a position that is 60min ago
		if t1 in range(24*60) and t2 in range(24*60) and pos[t-window][2] > 0.0 and pos[t+window][2] > 0.0 :
			straightDist = vincenty((pos[t-window][0], pos[t-window][1]), (pos[t+window][0], pos[t+window][1])).kilometers
			travelDist = sum(dist[t-window:t+window])
			if travelDist > 0:
				efficiencies.append(min(straightDist/travelDist, 1.0))
			else:
				efficiencies.append(0)
		else:
			efficiencies.append(0)
	
	#detect trips
	lowpass_eff = lowpass(efficiencies, 12.0) #moving_avg(efficiencies, 30) 
	tripstarts = []
	tripends = []
	
	t = 0
	while t < 24*60:
		if lowpass_eff[t] > 0.5: #trip detected, find start and end
			start = t
			while start > 0:
				if lowpass_eff[start] <= 0.2:
					break
				start = start - 1
			end = t
			while end < 24*60:
				if lowpass_eff[end] <= 0.2:
					break
				end = end + 1

			tripstarts.append(start)
			tripends.append(end)
			t = end + 1
		else:
			t = t + 1

	#plot trips
	startdates = mins2datenum(tripstarts)
	enddates = mins2datenum(tripends)
	yrange = [(s,e-s) for s,e in zip(startdates, enddates)]

	fig = plt.figure(3)
	ax = Subplot(fig,111)
	fig.add_subplot(ax)
	ax.axis["right"].set_visible(False)
	ax.axis["top"].set_visible(False)
	plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
	plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval=2))
	plt.xlim((mdates.date2num(config.SAMPLE_DAY_NO_TZ), mdates.date2num(config.SAMPLE_DAY_NO_TZ + datetime.timedelta(days=1))))
	plt.ylim((0,1.1))
	plt.gcf().autofmt_xdate()
	plt.ylabel("Efficiency (straightDist/travelDist)")
	plt.broken_barh(yrange, (0,1), facecolor = (1.0,0.5,0.0,0.4), linewidth = 0.0)

	#plot  efficiency
	times = [mdates.date2num(config.SAMPLE_DAY_NO_TZ + datetime.timedelta(minutes = float(m))) for m in range(24*60)]
	assert(len(efficiencies) == len(times))

	plt.plot(times, efficiencies,  linewidth = 2.0, color = "blue")
	plt.plot(times, lowpass_eff,  linewidth = 2.0, color = "red")

	plt.savefig("figs/eff/" + userid + ".png")
	plt.close()
Exemple #40
0
def draw_win(flow_num, open_time, close_time, hight):
    plt.broken_barh([(open_time, close_time - open_time)],
                    (hight * (flow_num * 2 + 1), hight))
Exemple #41
0
def make_tornado_plot(dataframe, factors, show=False, fig_path=None):
    dfs = os.listdir(dataframe)
    print 'pickled dfs: {}'.format(dfs)
    filename = 'norm_sensitivity.pkl'
    if filename in dfs:

        df = read_pickle(os.path.join(dataframe, filename))
        df.to_csv(os.path.join(fig_path, 'sample_norm_df.csv'))
        print df
        xx = 1

        for index, row in df.iterrows():
            print index, row
            base = row[0][5]
            lows = []
            for fact in row:
                lows.append(min(fact))
            lows = array(lows)
            values = []
            for fact in row:
                values.append(max(fact))

            # The y position for each variable
            ys = range(len(values))[::-1]  # top to bottom

            # Plot the bars, one by one
            for y, low, value in zip(ys, lows, values):
                # The width of the 'low' and 'high' pieces
                low_width = base - low
                high_width = abs(value - base)
                # Each bar is a "broken" horizontal bar chart
                plt.broken_barh(
                    [(low, low_width), (base, high_width)],
                    (y - 0.4, 0.8),
                    facecolors=['white',
                                'white'],  # Try different colors if you like
                    edgecolors=['black', 'black'],
                    linewidth=1)
                plt.subplots_adjust(left=0.32)

                # Display the value as text. It should be positioned in the center of
                # the 'high' bar, except if there isn't any room there, then it should be
                # next to bar instead.
                x = base + high_width / 2
                if x <= base:
                    x = base + high_width
                    # plt.text(x, y, str(round(value - low, 1)) + 'mm', va='center', ha='center')

            # Draw a vertical line down the middle
            plt.axvline(base, color='black')

            # Position the x-axis on the top, hide all the other spines (=axis lines)
            axes = plt.gca()  # (gca = get current axes)
            axes.spines['left'].set_visible(False)
            axes.spines['right'].set_visible(False)
            axes.spines['bottom'].set_visible(False)
            axes.xaxis.set_ticks_position('top')

            # Make the y-axis display the factors
            plt.yticks(ys, factors)
            print 'location: {}'.format(index)
            plt.title('{} [mm]'.format(index.replace('_', ' ')), y=1.05)
            # Set the portion of the x- and y-axes to show
            plt.xlim(min(-20, 1.2 * min(lows)), base + 1.1 * max(values))
            plt.ylim(-1, len(factors))
            # plt.show()
            if show:
                plt.show()
            # if fig_path:
            #     plt.savefig('{}_tornado'.format(index), fig_path, ext='jpg', dpi=500, close=True, verbose=True)
            plt.close()
Exemple #42
0
# rotor 0-7 position
# 0,2,4,6: positive yaw rotation
# 1,3,5,7: negative yaw rotation
loc[0] = np.array([0.25 * c, 2.5 * c, 0])
loc[1] = np.array([0.25 * c, 1.5 * c, 0])
loc[2] = np.array([0.25 * c, -1.5 * c, 0])
loc[3] = np.array([0.25 * c, -2.5 * c, 0])
loc[4] = np.array([-0.75 * c, -2.5 * c, 0])
loc[5] = np.array([-0.75 * c, -1.5 * c, 0])
loc[6] = np.array([-0.75 * c, 1.5 * c, 0])
loc[7] = np.array([-0.75 * c, 2.5 * c, 0])
thrust = np.array([0, 0, -kf])
moments = np.cross(loc, thrust)

plt.figure(num=1, figsize=(6, 6), dpi=100)
plt.broken_barh([(-0.5, 1.0)], (-3.0, 5.0), alpha=0.2)  # fuselage
plt.broken_barh([(-3.0, 6.0)], (-0.75, 1.0), alpha=0.2)  # wing
plt.broken_barh([(-1.2, 2.4)], (-3.0, 1.0), alpha=0.2)  # tail
plt.plot(loc[0::2, 1] / c,
         loc[0::2, 0] / c,
         'o',
         markersize=6,
         label='CW rotation')
plt.plot(loc[1::2, 1] / c,
         loc[1::2, 0] / c,
         'o',
         markersize=6,
         label='CCW rotation')
plt.plot(loc[0::2, 1] / c, loc[0::2, 0] / c, 'o', alpha=0.1, markersize=30)
plt.plot(loc[1::2, 1] / c, loc[1::2, 0] / c, 'o', alpha=0.1, markersize=30)