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

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')

Related

How to get string value centered above bars in bar chart

I am having trouble aligning the text above the correct bars in the following bar graph, I can't figure out where it's going wrong?
CODE:
bar(two_weeks,zAxis);
text(1:length(two_weeks),zAxis,num2str(zAxis),'vert','bottom','horiz','center');
box off
ylabel('Z Axis')
BAR CHART:
The arrows were added post production and are showing where they should be aligned to. Also note that I was too lazy to draw all of the arrows.
DATA:
two_weeks =
1×14 datetime array
[ 21-Nov-2018, 22-Nov-2018, 23-Nov-2018, 24-Nov-2018, 25-Nov-2018, 26-Nov-2018, 27-Nov-2018, ...
28-Nov-2018, 29-Nov-2018, 30-Nov-2018, 01-Dec-2018, 02-Dec-2018, 03-Dec-2018, 04-Dec-2018 ]
zAxis =
[ 5, 12, 1, 7, 13, 24, 2, 27, 62, 0, 3, 17, 74, 4 ].'
Your x axis is specified using a datetime array. What you're then using is guesswork to align indices (1:length(two_weeks)) for the x coordinates of your text items.
Instead, simply use the same datetime array for the position of the text!
bar( two_weeks, zAxis );
text( two_weeks, zAxis, arrayfun(#num2str,zAxis,'uni',0) );
As you did in the question, we want to set 'VerticalAlignment' to 'bottom' and 'HorizontalAlignment' to 'center' to neaten things up above the bars:
bar( two_weeks, zAxis );
text( two_weeks, zAxis, arrayfun(#num2str,zAxis,'uni',0), ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center' );
Output:

"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:

Julia: PlotlyJS and relative bar subplots

I have recently started using PlotlyJS.
I am trying to create a subplots of relative bar charts (bar charts with barmode=relative). Unfortunately it seems that the bar charts lose their barmode attribute, when they become a subplot. For example:
# PlotlyJS
using PlotlyJS;
pltjs = PlotlyJS
function bar1()
trace1 = pltjs.bar(;x=[1, 2, 3, 4], y=[1, 4, 9, 16], name="Trace1")
trace2 = pltjs.bar(;x=[1, 2, 3, 4], y=[6, -8, -4.5, 8], name="Trace2")
data = [trace1, trace2];
layout = pltjs.Layout(;xaxis_title="X axis",
yaxis_title="Y axis",
barmode="relative",
title="Relative 1");
pltjs.plot(data, layout)
end
function bar2()
trace3 = pltjs.bar(;x=[1, 2, 3, 4], y=[-15, -3, 4.5, -8], name="Trace3")
trace4 = pltjs.bar(;x=[1, 2, 3, 4], y=[-1, 3, -3, -4], name="Trace4")
data = [trace3, trace4];
layout = pltjs.Layout(;xaxis_title="X axis",
yaxis_title="Y axis",
barmode="relative",
title="Relative 2");
pltjs.plot(data, layout)
end
p1 = bar1();
pltjs.savefig(p1, "./fig1.pdf") # This works
p2 = bar2();
pltjs.savefig(p2, "./fig2.pdf") # This works
p3 = [bar1() bar2()];
pltjs.savefig(p3, "./fig3.pdf") # This doesn't works
I have seen similar issues in other languages, but I wasn't able to find a workaround for Julia. Would you please explain me why doesn't work and how to fix it?
I managed to do it using the code below:
p3.plot.layout["barmode"] = "relative";

ZXing library Reed Solomon example

I want to try the ReedSolomonDecoder from the ZXing library on the example given on page 10 of this paper
Basically, it encodes the message
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
using the generator polynomial
x^4 + 15x^3 + 3x^2 + x + 12
which results in
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12
I want to decode this in the following manner:
int[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 3, 12, 12};
GenericGF field = new GenericGF(?, 16, 1); // what integer should I use for primitive here?
ReedSolomonDecoder decoder = new ReedSolomonDecoder(field);
decoder.decode(data, 4);
I don't know how to create a GenericGF object from the given generator polynomial. I know that it expects a binary integer representation of the polynomial, but to do that, I would need the polynomial to be in an irreducible form, i.e. all the coefficients to be either 0 or 1. How can I achieve that from this given generator polynomial?
I'm pretty new to this as well but I think you would want to use
public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1);

Postive/Negative Chart in Google Visualization API

I need to generate a chart like this one:
Specifically, I want to show both a positive value and a negative value for a time period (could be an hour, minute, etc.) and display it like this.
I could have sworn I saw something like this on the Google Visualization API Gallery the other day, but I can't find it now, and am not even sure what this kind of chart is called.
First, do you know what this kind of chart is called so I can possibly find documentation? Second, is there any way to implement such a chart with the Google Visualization API? If not, is there another common charting solution for web that I can achieve this with?
Thank you for your time.
This is called a "Stacked Bar Chart", and can indeed be created with the Google Visualisation API.
Simply use the "isStacked" property (described here; http://code.google.com/apis/visualization/documentation/gallery/barchart.html).
Here's some sample code (based off the default bar chart example provided by Google and updated to show the use of isStacked and some sample data from your example);
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('number');
data.addColumn('number');
data.addRows(12);
data.setCell(0, 0, 'January');
data.setCell(1, 0, 'February');
data.setCell(2, 0, 'March');
data.setCell(3, 0, 'April');
data.setCell(4, 0, 'May');
data.setCell(5, 0, 'June');
data.setCell(6, 0, 'July');
data.setCell(7, 0, 'August');
data.setCell(8, 0, 'September');
data.setCell(9, 0, 'October');
data.setCell(10, 0, 'November');
data.setCell(11, 0, 'December');
data.setCell(0, 1, 19);
data.setCell(1, 1, 18);
data.setCell(2, 1, 20);
data.setCell(3, 1, 19);
data.setCell(4, 1, 18);
data.setCell(5, 1, 20);
data.setCell(6, 1, 19);
data.setCell(7, 1, 18);
data.setCell(8, 1, 20);
data.setCell(9, 1, 19);
data.setCell(10, 1, 18);
data.setCell(11, 1, 20);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
data.setCell(3, 2, -12);
data.setCell(4, 2, -13);
data.setCell(5, 2, -11);
data.setCell(6, 2, -12);
data.setCell(7, 2, -13);
data.setCell(8, 2, -11);
data.setCell(9, 2, -12);
data.setCell(10, 2, -13);
data.setCell(11, 2, -11);
data.setCell(0, 2, -12);
data.setCell(1, 2, -13);
data.setCell(2, 2, -11);
// Create and draw the visualization.
new google.visualization.ColumnChart(document.getElementById('visualization')).
draw(data,
{title:"S&P 500 Up/Down Performance Since 1980",
width:600, height:400,
isStacked:"true",
legend:"none" }
);
}
And the results...
Use ColumnChart instead of BarChart:
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
https://jsfiddle.net/0rrar9oq/16