Python Plotly Treemap Color Scale Variable Not Adding Up Child Values - colors

I am trying to plot treemap using Plotly in Python. I have three categorical variables that I am using as parent/child variables for the plot and two other continuous variables - "Sales_Quantity" and "Sales_Amount", and I am passing one of these as "value" - represented as area, and "color" variables respectively.
Here is the sample of my data-
{'Category': {1171: 'Cat_1049',
1172: 'Cat_1049',
1173: 'Cat_1049',
1174: 'Cat_1049',
1175: 'Cat_1049'},
'SubCategory_1': {1171: 6754, 1172: 6752, 1173: 6748, 1174: 6746, 1175: 6742},
'SubCategory_2': {1171: 'MCW-145',
1172: 'MCW-145',
1173: 'MCW-145',
1174: 'MCW-145',
1175: 'MCW-141'},
'Sales_Amount': {1171: 13509.043,
1172: 8662.54,
1173: 92531.29,
1174: 11312.07,
1175: 6427.037},
'Sales_Quantity': {1171: 6, 1172: 4, 1173: 38, 1174: 5, 1175: 3}}
I was expecting the values of "color" --> "Sales Amount" of all child variables to be added up and the color of the parent boxes displayed accordingly. However, this is not happening. And the value shown on hover on the parent nodes are often lower than the "Sales Amount" on individual child nodes.
Here is the code I used-
fig = px.treemap(df_test, path=
[px.Constant('All'),'Category', 'SubCategory_1','SubCategory_2'],
values='Sales_Quantity',
color='Sales_Amount',
color_continuous_scale=px.colors.sequential.Turbo)
fig.update_layout(legend_title="",
autosize=False,
width=1440,
height=900,
margin=dict(
l=0,
r=15,
b=10,
t=30,
pad=3
),title={'text':(f'Sales Amount and Quantities by Groups'),
'y':0.99,'x':0.5,'xanchor':'center',
'yanchor': 'top','font_size':20},
#title_x=0.5,
legend=dict(orientation="h"))
fig.show()
I am not sure if I am doing something wrong here or if the data is not structured properly. It does not make sense why it would take care of the aggregation on the "value" variable and not on the "color" variable.
For example, the sub-categories in "Cat_1049" have the highest "Sales Amount" of all, and the aggregate sales for this parent category is about $61.9 M. However, the color scheme is showing it as $5.4M.
Is there a way to make the colors appear the way I need it - which is for it expand the range of color scale and assign colors to the parent nodes accordingly?
I have tried some of the potential tweaks that could solve this from those found in treemap documentation - including branchvalues etc. Based on another tip - I made sure that all NaNs were removed, reorderdered the data-frame by the three categorical columns and reset the index. I also tried plotting using go.Treemap - which I could not even get it to display the plot in the first place.

Related

Precise anchoring of shadows on entities

I would have a question to ask, hoping for your advice. In my project I'm working on the different entities that will be present, one of them is the "TreeEntity". It is visibly composed of four parts. "Leaves, Shadow, Stump, Trunk". All four parts have only one id and are randomly chosen when a new "TreeEntity" is called. In addition, leaves and shadow each have four different states, based on the age of the tree. The shadow will have to rotate around the trunk based on the time. Now, since the engine of my project would like to create it as universal as possible, so that it can also be used in other projects or other people, I am facing a dead end.
I have this result at the moment (premise, all the images used are for testing purposes only):
As can be seen, the shadow is very detached from the trunk. This, theoretically, would be possible to fix it, modifying the image used:
Or write a huge dictionary with all the precise anchors written for each image that make up leaves, shadow, stump and trunk.
But doing so would be detrimental to my goal. So, the advice I would like to ask you, is whether in pyglet or otherwise, regardless of the shadow used (which in this case is random) is it possible to choose an anchor point that places it in the right place? Or do you have to modify the image or create a dictionary?
My question arises from the fact that I have seen several times images for example of shadows or something else that is like the one I inserted here, but in that case the shadows were in their correct place. The same game from here I took the pictures to test and I'm inspired by one or two mechanics has the shadows placed in the right place. So I don't know which way to go sincerely.
At the moment, all four parts have a universal anchor dictated by a small dictionary:
__statdict = {
"leaves": {
"cell": (3, 1),
"anchor_x": "center",
"anchor_y": "bottom"
},
"shadow": {
"cell": (4, 1),
"anchor_x": "left",
"anchor_y": "bottom"
},
"stump": {
"cell": (1, 1),
"anchor_x": "center",
"anchor_y": "bottom"
},
"trunk": {
"cell": (1, 1),
"anchor_x": "center",
"anchor_y": "bottom"
}
}
And when loading resources, anchor is assigned:
if self.__statdict[name]["anchor_x"] == "center":
ancx = texture.width // 2
elif self.__statdict[name]["anchor_x"] == "right":
ancx = texture.width
else:
ancx = 0
if self.__statdict[name]["anchor_y"] == "center":
ancy = texture.height // 2
elif self.__statdict[name]["anchor_y"] == "top":
ancy = texture.height
else:
ancy = 0
texture.anchor_x = ancx
texture.anchor_y = ancy
I have tried to be as complete as possible. Thanks for your help. Maybe maybe I'm having problems for nothing and the solution is simpler than I thought, so I apologize if this were the case.
Update (1):
Is it possible, via pyglet or a module to accompany it, given the direction of a sprite (in this case on the right), to know what is the position of the first pixel that is not an alpha? Something like this: "The direction of the SpriteShadow is to the right. The first pixel that has an rbg value with alpha equal to 255 is ay = 20 (relative to the width of the sprite). Then I move the SpriteShadow to the left with respect to the general anchor of 20 pixels "

Once Shapefile is imported to Matlab, How to use longitude and latitude data from the created structure

I created a shapefie from a portal item in Arc GIS. So, on Matlab, I have this map, which shows the polygons outlined within NYC:
MapNYC
The shapefile I have is called "Sewershed.shp".
In Matlab, I type in:
shaperead('Sewershed');
Then, I get a 17x1 structure
17x1 Structure
So, the heading, "Bounding Box" are the longitude and latitude boundaries that define the Sewersheds (under heading "SewerShed")
The polygons in the map I showed above are the outlines of the sewer shed.
Now, I have an Excel spreadsheet that lists items as defined by longitude latitudes. I need to identify which Sewer Shed each item belongs to.
Excel Spreadsheet Image
I'd appreciate any help with this!
Josephina
assuming your boundingbox record has [x0,y0, x1,y1] where x0-x1 is the range of the longitude and y0-y1 is the range of your latitude. If you have a new coordinate newpt=[long1, lat1], you just need to compare long1 with x0-x1 and lat1 with y0-y1 and find the records that they belong to.
here is my sample test code
mydata(1).BoundingBox=[1,2,5,7]
mydata(2).BoundingBox=[2,3,6,9];
mydata(3).BoundingBox=[7,1,8,20];
bbx=cell2mat({mydata(:).BoundingBox}')
newpt=[4,3]
find(newpt(1)>=bbx(:,1) & newpt(1)<=bbx(:,3) & newpt(2)>=bbx(:,2) & newpt(2)<=bbx(:,4))

IcCube - Leaves in Pivot table

If I have a parent-child-dimension, whose data I show in the IcCube/PivotTable and I want to show the leaves only (with descendants([categories].[categories].[All-M],,leaves)), those leaves are shown hierarchically. So a level5 leave of one category is suddenly the parent of a level6 leave of the next category.
Is there a way to "flatten" this tree, so that those leaves are all shown on one level and not sorted in the tree wrongly?
EDIT:
Here is a query:
SELECT
NON EMPTY { [Measures].[group_quality] } ON COLUMNS,
NON EMPTY { TopCount(descendants([categories].[categories].[Level$0].[Portal],,leaves),30,[Measures].[revenue_potential]) } ON ROWS
FROM [Cube]
All categories in the picture are leaves, so none of them has any children.
As workaround for that case you can use simple CSS dirty-hack.
1) First of all under Widget option tab of your widget you should set Hide icons to yes.
2) After this, to avoid collision with other Pivot Tables, add custom CSS class
3) Add these styles to Report CSS.
.pt-flattened .pt-hcont{
margin-left: 0 !important
}

Flot data serie with specific color per data element?

being new to Flot i am struggling a bit. My goal is the present a bar with different data elements in it that must have a different color per element. I want to provide the color per data element.
Any hints on how this can be done?
Example:
[0,100][0,200][0,100][0,200]
All elements with value 100 should be blue and all elements with 200 should be green.
A nice one would be,
[0,100,blue][0,200,green][0,100,blue][0,200,green]
But this off course does not work, it is just an explanation what i want to achieve!
Doing this with multiple data series seems does not work in my case.
Any hints on how this can be done?
You can do it with multiple series, as this is how I have done something like this :)
So in your plot method you would have something like this:
$.plot($('#placeholder'), []); // The array would hold your data
You can also further extend this to provide some options to flot to tell it what to make your chart look like. To do that you pass an object of options after the array.
However, im not quite sure what you need in terms of the Graph, your example gave some numbers but the x coordinate was all 0, im not sure if you just want a bar graph?
Anyway, heres the code of how you would get it to display a bar graph, with one green line and one blue line:
var flotOptions = {
series: {
lines: { show: true, fill: false, lineWidth: 15 }
}
},
var data = [{
color: '#001EFF',
data: [[0, 0], [0, 100]]
}, {
color: '#00FF0E',
data: [[5, 0], [5, 200]]
}];
$.plot($('#placeholder'), data, flotOptions);
I would recommend making it so that the data is automatically generated on a server side, then you can check in javascript, and add the color depending on the y value in the data array of each series.
I have also created a jsFiddle of this for you, so you can go take a look and play around with it. As I have said im not quite sure what you want but this is a good start for you. Good luck!!

Highcharts manipulate with points on the graph

I'm looking for a way to access each point's on the graph actual svg object in order to change some of its properties on-the fly.
Say I have 100 objects, of those around 50% need to be drawn in a different color (though they belong to the same data series, no way round). Thus, by using:
chart.series[0].points
I'm able to modify object properties (like fill) but no way to redraw the chart afterwards. Using chart.redraw() gives nothing, well actually it changes the points color but only after I mouse over them, the chart itself is not redrawn properly.
Any help appreciated.
Regards!
There are at least two ways that you can use here.
On Load
You can define a color for each of the points explicitly while adding them to the data array. The following is what the documentation states
A list of object with named values. In this case the objects are point configuration objects as seen below.
Range series values are given by low and high.
Example:
data: [{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
Reference: http://api.highcharts.com/highcharts#series.data
jsFiddle # http://jsfiddle.net/jugal/mXPPH/
Dynamically
You can add the color/markers to individual points using the point.update() method as follows,
var p = chart.series[0].points[l - 1];
p.update({
marker: {
symbol: 'square',
fillColor: "#A0F",
lineColor: "A0F0",
radius: 5
}
});
Reference : http://api.highcharts.com/highcharts#Point.update%28%29
You may also want to look a similar answer I had provided for this question # Dynamically draw marker on last point in highcharts and its jsFiddle # http://jsfiddle.net/jugal/zJZSx/

Resources