Grouped bar chart -- how to make sns respect order of rows? - seaborn

I have a multilevel data frame, sorted by values on level=1
I want to make a grouped bar plot. When I do
ax = sns.barplot(ax=ax, x="level0", y="values",
hue="level1", data=data)
Sns decides to sort index by level1.
Is there a way to make sns respect the order of rows when making grouped bar plots?

As per the comment from #ImportanceOfBeingErnest, you can use the order and hue_order parameters to control in which order the bars appear along the x-axis.

Related

PowerBI groupby with filters

My company has tasked with slicing the information on turnover and to create different graphs.
My source data looks like this: Relevant columns are: Voluntary/Involuntary, Termination Reason, Country, Production, and TermDateKey
I am trying to get counts using different filters on the data. I managed to get the basic monthly total using the formula:
Term Month Count = GROUPBY('Turnover Source','Turnover Source'[TermDateKey],"Turnover Total Count", COUNTX(CURRENTGROUP(),'Turnover Source'[TermDateKey]))
This gave me a new sheet with the counts for each month.
Table that shows TermDateKey on Column 1, and Counts on column 2
I am trying to add onto this table by adding counts but using different filters.
For example, I am trying to add another column that gives me the monthly count but filtered for 'Turnover Source'[Voluntary/Involuntary]=="Voluntary". Then another column for 'Turnover Source'[Voluntary/Involuntary]=="Involuntary" and so on. I have not found anywhere that shows me how to do this and when I add in the FILTER function it says that GROUPBY(...) can only work on CURRENTGROUP().
Can some one point me to a resource that will give me the solution I need? I am at a loss, thank you all.
It looks like you may not be aware that you don't have to calculate all possible groupings with DAX formulas.
The very nature of Power BI is that you use a column like "Termination Reason" on an X axis or in the legend of a visual. Any measure that you have created on values of another column, for e.g. a count of all rows, will then automatically be calculated to be grouped by the values in "Termination Reason", giving you a count of each of the values in the column.
You do NOT need DAX functions to calculate the grouping values for each measure for each column value combination.
Here is some simple sample data that has been grouped into dates and colours, one chart showing a count of each colour and one chart showing a sum of the Value column. No DAX was written for that.
If your scenario is different, please explain.

Kibana, filter data on the basis of one field and then grouping values on basis of timestamp (yearly)

I have three fields in my data: tran_date, use_case, Amount.
use_case field having multiple values i.e. B2B, cash_in, C2C etc.
I want to plot a bar chart by sum up Amount field against use_case (B2B, cash_in only) then want to group data on yearly basis . so there will be a bar for each year summing up the Amount against use case B2B, cash_in.
I explored filter and sub-buckets but they don't seem to provide grouping of values.
"sub buckets" create aggregations, which are groupings. Setup your x-axis as a Date Histogram on #timestamp, then add a sub-bucket using Terms for use_case. To get the sum, edit the y-axis (metrics).

Tableau calculated-field filter on pie-chart doesn't work

Based on previous question, I had to create calculated value for Location, and use that as quick filter, i.e.
Location Filter:
LOOKUP(ATTR([Location (Loc)]),0)
Workbook is on Public Tableau
For hovering over points in a map, the calculated field works, but when I create pie chart, it doesn't work.
For instance, if I select All, this is the result
And if I select a business from Location Filter, this is what I get
How to troubleshoot?
Additional Info
However, if I use regular Location filter, then it works, i.e
There are two separate issues to address here:
LOOKUP(ATTR([Location (Loc)]),0) is a sneaky way of filtering the data in the view while still maintaining all of the locations in the partition (by disguising the field as a table calculation, the filtered partition is created before this table calculation is ever executed). Because you've used it here, you still have every location in the partition, even when you filter them out with the quick filter. Because they're still in the partition, when you calculate the percent of total, those other locations will be included in that total, even if they're not displayed in the view.
I don't see a reason for you to keep all of the locations in your partition in this case, so I'd just replace that filter with [Location].
It looks like you've dragged [Location] into your mark as a dimension. As a result, it's broken up the pie slices into smaller chunks, one per location. If you add a dimension to your data, then Tableau will have to group by that dimension when calculating the aggregations.
If you want the Location to appear in the tooltip of your pie chart, you'll have to either add it as an attribute (in which case you'll have to deal with the "*" when you have more than one location in the partition), or you'll just have to deal with the slices being broken up further.

Accessing non-axis data attributes in dimple.js

One of my favorite aspects of d3 is that it groups collections of data that can be accessed easily at any point in the process of creating and drawing a visualization.
I picked up Dimple to build some prettier graphs, implementing the following example:
http://dimplejs.org/advanced_examples_viewer.html?id=advanced_dynamic_line_color
I'd like to access an ID attribute of my data in an onclick event associated with the data points. Unfortunately, the only data passed to these events by dimple is the axis coordinates.
Is there a simple way to get at the rest of the attributes of the data in dimple?
The reason is that dimple will pre-aggregate the data so it doesn't work from the original data rows. Virtually all of the examples on the dimple website are drawn from the same tsv and in each case dimple aggregates the data to different levels. The benefit of this is that it means you can use very similar code for a line, area, bubble, bar or pie chart at any level of aggregation without any data manipulation in your own code.
Therefore if you would like another field it must be part of the aggregation, you can include as many fields as you like in the the series definition so:
var lines = myChart.addSeries(null, dimple.plot.line);
could become:
var lines = myChart.addSeries(["Brand", "Owner", "SKU", "Product"], dimple.plot.line);
And those 4 fields would be accessible.
Beware though that a line will be drawn for every distinct combination of those dimension values so you can't use this method to access a more granular datum.

Joining grouped tables

I have two different scripted data sets that I am pulling data from and aggregating (on the same key). What I want to do is to display one one line the aggregated data from both sources. The data is coming from a scripted data source (POJOs).
A simplified example is given below in which an Order has many Components, with each component being for a different customer at a different quoted price. Then when each Order is filled in different lots (or Fills) at different prices. I want to be able to produce a summary of each Order with the total Ordered and Filled quantity, and the weighted average quoted price and filled price.
An Order Component table
Order ID, Customer Num, Qty, Quoted Px
Ord01,Cust01,3,100
Ord01,Cust02,3,102
Ord02,Cust01,5,200
Ord02,Cust03,5,204
And then a Order Fullfillment table
OrderID,FillId,Qty,CostPx
Ord01,F01,4,100
Ord01,F02,2,106
Ord02,F03,2,200
Ord02,F04,8,210'
I would like to display something like this:
Order ID, Order Qty, Fill Qty, Avg Order Px, Avg Fill Px
Ord01, 6, 6, 101, 102
Ord02, 10, 10, 202, 208
I've tried using subreports and that seems to be able to get me the results but in a terrible format. The subtable headers repeat so every order gets it's own headers.
You may want to create a BIRT joined dataset between your two scripted datasets, based on a full outer join on the "order ID" column, and then use this joined dataset in your report. It should meet your needs.
I solved my problem by more or less following the following guide.
So I created a List linked to my first data source. I then added a group on Order ID so that I had one list row per Order. In the group header I added a 2x1 grid, I placed a table of the Order Components into one side of the grid and a table of Fills into the other. I had to add filters to both of these so that they only contained data for the correct OrderId. I then grouped the tables by OrderId, added my aggregation fields.
All that is left is to set the visibilty. So I set the visibility of the table details to false. In order to only show the table header once (instead of once per order) I added a Running Count aggregation to the List and set the visibiity to invisible when this aggregation was greater than 1.
Was actually quite easy in the end but took me ages to work out how to do it.

Resources