How can I show the legend to all bars in Matlab? - matlab

Below in the figure, I am trying to show the legend to all bars, but I only get the legend for the first bar. How can I show it to all of them?
figure
CoverageArea = [101.1303,0,114.9316,45.2112,116.5973,95.8953];
BarNames = {'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'};
h = bar(CoverageArea,'stacked');
ylabel('Coverage area (m²)');
xticklabels({'\Phi_1_/_2 = 15, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 15, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 30, (\phi & \psi) =\pm 40','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 10','\Phi_1_/_2 = 60, (\phi & \psi) =\pm 40'})
colors = get(gca(), 'ColorOrder' ); % use whatever colors you want here
set(h, 'FaceColor' , 'flat' , 'CData' ,colors(1:6,:)) % set the bars' colors
hLg = legend(BarNames,'Location','best');
grid on;

Related

How to extract a specific timeframe from datetime?

I have the time in datetime format like below. I would like to extract the time from '20-Apr-2020 11:20:10' till '20-Apr-2020 12:40:50'. Do I need it to convert it first to datenumber or I can do it directly here?
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30','20-Apr-
2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
Time_datetime = datetime(Time_One,'InputFormat','dd-MM-yyyy HH:mm:ss');
Time_datenum={'20-Apr-2020 11:06:00','20-Apr-2020 11:20:10','20-Apr-2020 11:45:30',...
'20-Apr-2020 12:07:00','20-Apr-2020 12:35:40','20-Apr-2020 12:40:50','20-Apr-2020 13:07:00'};
% Create a datetime array from a cell array of character vectors.
Time_datetime = datetime(Time_datenum, 'InputFormat', 'dd-MM-yyyy HH:mm:ss', 'Locale', 'en_GB');
% t = datetime(Year, Month, Day, Hour, Minute, Second)
Time_start = datetime(2020, 4, 20, 11, 20, 10);
Time_end = datetime(2020, 4, 20, 12, 40, 50);
% Extract the time.
Time_extracted = Time_datetime(Time_start <= Time_datetime & Time_datetime <= Time_end);

"Impossible" error message in R for VennDiagram

I keep getting an error message when trying to use VennDiagram in R. Below is my posted code:
draw.quintuple.venn(area1 = 578, area2 = 519, area3 = 212, area4 = 402, area5 = 172, n12 = 366, n15 = 97, n13 =149, n14 = 284, n23 = 103, n24 = 202, n25 = 125, n35 = 31, n34= 12, n45 = 27, n123 = 80, n124 = 161, n125 = 84, n134 = 8, n135 = 25, n145 = 20, n234 = 5, n235 = 24, n245 = 21, n345 = 1, n1234 = 5, n1345 = 21, n1245 = 16, n1235 = 0, n2345 = 0, n12345 = 0, category = c("1", "2", "3", "4", "5"), lty = "blank", fill = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"))
Error:
ERROR [2018-07-09 13:37:19] Impossible: a11 <- n23 - a21 - a22 - a24 -
a26 - a29 - a30 - a31 produces negative area Error in
draw.quintuple.venn(area1 = 578, area2 = 519, area3 = 212, area4 =
402, : Impossible: a11 <- n23 - a21 - a22 - a24 - a26 - a29 - a30
- a31 produces negative area
What am I doing wrong?
I double checked and made sure the values are all correct.
I do not think that this package is well documented. A look at the source code shows that the meaning of nxxxxx is not the obvious one. For instance n135 means "how many elements belong to at least groups 1, 3 and 5". When you want to draw the diagram, the package calculates how many of those n135 also belong to other groups (i. e., n1235, n1345 and n12345) and substracts them.
What seems to be happening here is that you interpret n135 as "how many elements only belong to sets 1, 3 and 5" (that would have also been my guess). If you want to use those numbers directly, you should write:
draw.quintuple.venn(area.vector = c(578, 519, 212, 402, 172, 31, 97, 284, 366, 125, 103, 149, 12, 202, 27, 1, 25, 20, 161, 84, 24, 80, 8, 5, 21, 0, 21, 16, 0, 5, 0), category = c("1", "2", "3", "4", "5"), lty = "blank", fill = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"), direct.area = T)
The order of the numbers is taken directly from the source code, I have not seen it documented. Let us call a135 "how many elements only belong to sets 2, 3 and 5". With this in mind, the order would be:
a1, a2, a3, a4, a5, a35, a15, a14, a12, a25, a23, a13, a34, a24, a45, a345, a135, a145, a124, a125, a235, a123, a134, a234, a245, a2345, a1345, a1245, a1235, a1234, a12345
If you prefer to use the n135 notation, you would need to transform your data, so that n135 = a135 + a1235 + a1345 + a12345, and so forth. In your case, n135 = 25 + 0 + 21 + 0 = 36.
Although not part of the question, you can also use my nVennR package for a proportional representation. The order of the numbers is documented in the help and the vignette, and you can also enter raw sets rather than numbers:
library(nVennR)
myV <- createVennObj(nSets = 5, sNames = c('1', '2', '3', '4', '5'), sSizes = c(0, 172, 402, 27, 212, 31, 12, 1, 519, 125, 202, 21, 103, 24, 5, 0, 578, 97, 284, 20, 149, 25, 8, 21, 366, 84, 161, 16, 80, 0, 5, 0))
myV <- plotVenn(nVennObj = myV, setColors = c("skyblue", "pink1", "mediumorchid", "yellow", "orange"), borderWidth = 0)
And the result:

Why does _DateDiff() return 0?

My AutoIt script should predict when a process will complete by saying The process will be complete at 3:18:24 PM. But that is not happening. It takes these input values:
Starting time
Starting percentage
Current time
Current percentage
It displays:
"The extraction will be complete at 0".
I isolated the problem to _DateDiff(). I thought the first parameter was n for difference in minutes. But it returns 0 when I pass the next two parameters (date values spaced sixty minutes apart). Here's the code:
GUICreate("Completion Time Predictor", 300, 300)
;Here I enter the starting time and percentage
GUICtrlCreateLabel("Enter the start time:", 10, 10, 270, 29)
$StartTime = GUICtrlCreateDate("", 10, 40, 270, 21, $DTS_TIMEFORMAT)
GUICtrlCreateLabel("Enter the start percentage:", 10, 70, 270, 29)
Local $StartPercent = GUICtrlCreateInput("", 10, 100, 270, 21)
;Here I enter the current time and percentage
GUICtrlCreateLabel("Enter the current time:", 10, 130, 270, 29)
$CurrentTime = GUICtrlCreateDate("", 10, 160, 270, 21, $DTS_TIMEFORMAT)
GUICtrlCreateLabel("Enter the current percentage:", 10, 190, 270, 29)
Local $CurrentPercent = GUICtrlCreateInput("", 10, 220, 270, 21)
Local $CTRL_a = GUICtrlCreateButton("Calculate", 10, 260, 280, 21)
GUISetState()
Local $msg
Do
$msg = GUIGetMsg()
Select
Case $msg = $CTRL_a
$PercentChange = GUICtrlRead($CurrentPercent) - GUICtrlRead($StartPercent)
;This seems to be where everything is going wrong
$ChangePerMinute = $PercentChange / _DateDiff('n', GUICtrlRead($StartTime), GUICtrlRead($CurrentTime))
$MinutesRemaining = (100 - GUICtrlRead($CurrentPercent)) / $ChangePerMinute
$EndTime = _DateAdd('n', $MinutesRemaining, GUICtrlRead($CurrentTime))
MsgBox($MB_OK, "Predicted Completion Time:", "The extraction will be complete at " & $EndTime)
EndSelect
Until $msg = $GUI_EVENT_CLOSE
I have no idea what you are doing, but maybe this helps you finding the problem
#include <Date.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
GUICreate("Completion Time Predictor", 300, 300)
;Here I enter the starting time and percentage
GUICtrlCreateLabel("Enter the start time:", 10, 10, 270, 29)
$StartTime = GUICtrlCreateDate("", 10, 40, 270, 21, $DTS_TIMEFORMAT)
GUICtrlCreateLabel("Enter the start percentage:", 10, 70, 270, 29)
Local $StartPercent = GUICtrlCreateInput("0", 10, 100, 270, 21)
;Here I enter the current time and percentage
GUICtrlCreateLabel("Enter the current time:", 10, 130, 270, 29)
$CurrentTime = GUICtrlCreateDate("", 10, 160, 270, 21, $DTS_TIMEFORMAT)
GUICtrlSetData(-1, _NowCalcDate() & ' ' & #HOUR + 1 & ":" & #MIN & ':' & #SEC)
GUICtrlCreateLabel("Enter the current percentage:", 10, 190, 270, 29)
Local $CurrentPercent = GUICtrlCreateInput("10", 10, 220, 270, 21)
Local $CTRL_a = GUICtrlCreateButton("Calculate", 10, 260, 280, 21)
GUISetState()
Local $msg
Do
$msg = GUIGetMsg()
Select
Case $msg = $CTRL_a
ConsoleWrite(_NowCalcDate() & ' ' & GUICtrlRead($StartTime) & " " & _NowCalcDate() & ' ' & GUICtrlRead($CurrentTime) & #CRLF)
$PercentChange = GUICtrlRead($CurrentPercent) - GUICtrlRead($StartPercent)
ConsoleWrite("% Change: " & $PercentChange & #CRLF)
;This seems to be where everything is going wrong
$diff_min = _DateDiff('n', _NowCalcDate() & ' ' & GUICtrlRead($StartTime), _NowCalcDate() & ' ' & GUICtrlRead($CurrentTime))
ConsoleWrite("Diff in Min: " & $diff_min & #CRLF)
$ChangePerMinute = $PercentChange / $diff_min
ConsoleWrite("Change / min : " & $ChangePerMinute & #CRLF)
$MinutesRemaining = (100 - GUICtrlRead($CurrentPercent)) / $ChangePerMinute
ConsoleWrite("MinutesRemaining : " & " " & $MinutesRemaining & #CRLF)
$EndTime = _DateAdd('n', $MinutesRemaining, _NowCalcDate() & ' ' & GUICtrlRead($CurrentTime))
MsgBox($MB_OK, "Predicted Completion Time:", "The extraction will be complete at " & $EndTime)
EndSelect
Until $msg = $GUI_EVENT_CLOSE

Use words as tick labels with unequal intervals in D3.js

I am creating a D3.js plot of multiple years of data. I would like to use the month names as the the axis labels. I am currently using the number of days into the year corresponding with the first day of that month as the labels as shown in the screenshot of my x-axis:
I believe all the relevant code is shown below:
var margins = {'top' : 20, 'right' : 20, 'bottom' : 50, 'left' : 70};
var height = 500;
var width = 960;
var plotHeight = height - margins.top - margins.bottom;
var plotWidth = width - margins.right - margins.left;
var x = d3.scaleLinear()
.domain([0,366])
.range([margins.left, plotWidth]);
// I am not concerned with leap years
var monthDays = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var monthAbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var svg = d3.select('.svg').attr('width',width).attr('height',height);
svg.append('g').attr('transform', 'translate(0,' + plotHeight + ')').call(d3.axisBottom(x).tickValues(monthDays));
Is there some way to map the position on the x-axis to the correct month (i.e.: 31 to Feb.)? I have tried using d3.timeMonth/d3.timeMonths and ordinal scales but haven't found something suitable yet.
You can use tickFormat:
.tickFormat(function(d,i){ return monthAbr[i]})
Here is a working demo based on your code:
var margins = {'top' : 20, 'right' : 20, 'bottom' : 50, 'left' : 20};
var height = 100;
var width = 600;
var plotHeight = height - margins.top - margins.bottom;
var plotWidth = width - margins.right - margins.left;
var x = d3.scaleLinear()
.domain([0,366])
.range([margins.left, plotWidth]);
// I am not concerned with leap years
var monthDays = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var monthAbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var svg = d3.select('body').append("svg").attr('width',width).attr('height',height);
svg.append('g').attr('transform', 'translate(0,' + plotHeight + ')').call(d3.axisBottom(x).tickValues(monthDays).tickFormat(function(d,i){ return monthAbr[i]}));
<script src="https://d3js.org/d3.v4.min.js"></script>

How to a stacked bar chart with line chart using Plotly?

https://plot.ly/python/bar-charts/#bar-chart-with-line-plot
I want to create a bar chart with line plot like in the example above using plotly and iPython. On the other hand, I want the bar chart to be a horizontal stacked bar chart like in the example below using plotly and iPython. How do I do this?
https://plot.ly/python/bar-charts/#colored-bar-chart
y_saving_yes = [1, 2, 4, 6, 7, 7]
y_saving_no = [10, 10, 10, 10, 10, 10]
y_net_worth = [93453, 81666, 69889, 78381, 141395, 92969]
x_saving = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']
x_net_worth = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']
trace1 = Bar(
x=y_saving,
y=x_saving,
marker=Marker(
color='rgba(50, 171, 96, 0.6)',
line=Line(
color='rgba(50, 171, 96, 1.0)',
width=1,
),
),
name='Highlight Properties',
orientation='h',
)
trace2 = Bar(
x=y_saving,
y=x_saving,
marker=Marker(
color='rgba(50, 171, 96, 0.6)',
line=Line(
color='rgba(50, 171, 96, 1.0)',
width=1,
),
),
name='Highlight Properties',
orientation='h',
)
data = Data([trace1, trace2])
layout = Layout(barmode='stack')
fig1 = Figure(data=data, layout=layout)
trace3 = Scatter(
x=y_net_worth,
y=x_net_worth,
mode='lines+markers',
line=Line(
color='rgb(128, 0, 128)',
),
name='Highlight Views',
)
fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
shared_yaxes=False, vertical_spacing=0.001)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace3, 1, 2)
fig['layout'].update(layout)
py.iplot(fig, filename='oecd-networth-saving-bar-line')
Andrew from Plotly here. Super close! I think you just missed a fig.append_trace(trace2, 1, 1). Here's a simple example doing basically the same thing for reference.
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import Bar, Data, Figure, Layout, Marker, Scatter
x_0 = [1, 2, 4, 6, 7, 7]
x_1 = [10, 10, 10, 10, 10, 10]
y_0 = [2, 3, 4, 2, 3, 3]
trace1 = Bar(
x=x_0,
marker=Marker(color='#001f3f'),
orientation='h',
)
trace2 = Bar(
x=x_1,
marker=Marker(color='#0074D9'),
orientation='h',
)
trace3 = Scatter(y=y_0)
fig = tools.make_subplots(1, 2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 1, 2)
fig['layout'].update(barmode='stack')
py.iplot(fig, filename='oecd-networth-saving-bar-line')