matplotlib graph legend is in reverse order (barh)
I have 15 graphs that looks like this:
As you see, the legend is in reverse order. I want to see 4 first, below that 3 etc.
My code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def plot_bars_by_data(data, title):
fig, axs = plt.subplots(8,2, figsize=(20,40))
fig.suptitle(title, fontsize=20)
fig.subplots_adjust(top=0.95)
plt.rcParams.update({'font.size': 13})
axs[7,1].remove()
column_index = 0
for ax_line in axs:
for ax in ax_line:
if column_index < len(data.columns):
column_name = data.columns[column_index]
current_column_values = data[column_name].value_counts().sort_index()
axx = ax.barh([str(i) for i in current_column_values.index],
list(current_column_values.values),
color=[colors[i] for i in range(data[column_name].value_counts().count())])
ax.legend(axx, [str(i) for i in current_column_values.index])
ax.set_title(column_name)
column_index +=1
plt.show()
# random data
df_test = pd.DataFrame([np.random.randint(2,5,size=15) for i in range(15)], columns=list('abcdefghijlmnop'))
plot_bars_by_data(df_test, "testing")
I tried to just reverse the legend list, but it is reversing the colors as well. I'm using Python 3.6 in Jupyter python notebook. Any ideas?
See also questions close to this topic

reducer to find the most popular movie for each age group in python
I am trying to write mapper reducer for Hadoop to find the movies with 5 rating "the popular movies" for each age group.
I write this
mapper.py
to join the tow data set with the user Id to get the age from user data and the rating with the movie name from the rating data set .!/usr/bin/env python:
import sys for line in sys.stdin: # remove leading and trailing whitespace line = line.strip() line = line.split("::") rating = "1" movie = "1" user = "1" age = "1" if len(line) == 4 : #ratings data rating = line[2] movie = line[1] user = line[0] #print '%s %s %s' % (user,movie,rating) else: # users data user = line[0] age = line[2] print '%s\t%s\t%s\t%s' % (user,age,rating,movie)
this is the data structure rating data: userid, movieid, rating, timestamp user data: userid, gender, age, occupation
The reducer I wrote is not working at all it gave me 0 result.
I want the result to be the top popular movies for each age group:
1 2254 4567 18 8732 0987 0986 25 7654 8765 7658 35 6543 7645 7654 45 7654 8765 5433 50 7652 1876 7654 56 3986 3956

How to compare two columns from two DFs keeping some column constants and print row?
I'm working on a project where I have to find the changes done in second sheet (specific column) as compare to primary/Master sheet. after that I wanted to print or save the complete row in which changes are found. here are more details. both the excel sheets have many columns my master sheet has data something like as follows:
TID LOC HECI RR UNIT SUBD S EUSE INV ACT CAC FMT CKT DD SCID CUSTOMER F&E/SERVICE ID BVAP PORD AUTH RULE ST RGN CHCGILDTO3P050101D CHCGILDTO3P M3MSA0S1RA 0501.01D 1A1 IE D STR3RA8 S CL/HFFS/688898 /LGT 20180721 BLOOMBERG LP DS316668545 WMS881282 E.485339 IL N CHCGILDTO3P050101D CHCGILDTO3P M3MSA0S1RA 0501.01D 1A2 IE J DNA UNDER DECOM EID 2466 20190322 WMS881282 E.485339 IL N CHCGILDTO3P050101D CHCGILDTO3P M3MSA0S1RA 0501.01D 1A3 IE J DNA UNDER DECOM EID 2466 20190322 WMS881282 E.485339 IL N CHCGILDTO3P050101D CHCGILDTO3P M3MSA0S1RA 0501.01D 1A4 IE J DNA UNDER DECOM EID 2466 20190322 WMS881282 E.485339 IL N CHCGILDTO3P050101D CHCGILDTO3P M3MSA0S1RA 0501.01D 1A5 IE J DNA UNDER DECOM EID 2466 20190322 WMS881282 E.485339 IL N
and my second sheet has data as follows :
HECI UNIT INV SUB ACT CKT PACT DD LOC RR M3MSA0S1RA 1A1 IE $ CL/HFFS/688898 /LGT D 72118 CHCGILDTO3P 0501.01D M3MSA0S1RA 1A2 IE J DNA UNDER DECOM EID 2466 32219 CHCGILDTO3P 0501.01D M3MSA0S1RA 1A3 IE J DNA UNDER DECOM EID 2466 32219 CHCGILDTO3P 0501.01D M3MSA0S1RA 1A4 IE J DNA UNDER DECOM EID 2466 32219 CHCGILDTO3P 0501.01D M3MSA0S1RA 1A5 IE J DNA UNDER DECOM EID 2466 32219 CHCGILDTO3P 0501.01D
so first i want to check if the values of LOC HECI RR & UNIT are same in both the sheets I want to move forward and comapre ACT column and print the difference as output.
for example you can see row #1 in Master data ACT is 'D' and where as in second sheet its changes to '$'
so I want output something like related complete row which says its changes from 'D' to '$'
this seems very complicated to me as I'm at beginning stage of python and pandas.
I tried using loops but unable to execute also if I use too much loop that's not the pandas way I believe
here is my code:
import pandas as pd df1 = pd.read_excel("Master Database.xlsx") df2 = pd.read_excel("CHCGILDTO3P_0501.01D.xlsx") d1_act = df1['ACT'] d2_act = df2['ACT'] for index1, row1 in df1.iterrows(): for index2, row2 in df2.iterrows(): if(row1['LOC'],row1['HECI'],row1['RR']) ==(row2['LOC'],row2['HECI'],row2['RR']): for x in d1_act and y in d2_act: #print(x,y) if x != y: print (x, y) # not getting how to print complete respective row else: pass else: pass
I want ouput like:
M3MSA0S1RA 1A1 IE $ CL/HFFS/688898 /LGT D 72118 CHCGILDTO3P 0501.01D
changes from 'D to '$'
please assist ! thank you in advance!

merge duplicate cells of a column
My Current excel looks like:
  Type  Val    A  1    A  2    B  3    B  4    B  5    C  6 

This is the required excel:
  Type  Val  Sum    A  1  3       2     B  3  12       4        5     C  6  6  
Is it possible in python using pandas or any other module?

matplotlib: how to change format of decimal numbers on axis labels
The following code gives the output as:
import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame() df['A'] = pd.Series(np.random.uniform(0.1,0.6,size=(5))) df['B'] = pd.Series(np.random.uniform(0.1,0.6,size=(5))) fig, ax = plt.subplots() ax.barh(np.arange(0, len(df)), df['A'], height=0.3) ax.barh(np.arange(0.3, len(df) + 0.3), df['B'], height=0.3) plt.show()
I would like to change the xaxis tick labels such that they become:
0 .1 .2 .3 .4 .5 .6

Dataframe.plot() not working when ax is defined
I am trying to emulate the span selector for the data I have according to the example shown here (https://matplotlib.org/examples/widgets/span_selector.html). However, my data is in a dataframe & not an array. When I plot the data by itself with the using the code below
input_month='201706' plt.close('all') KPI_ue_data.loc[input_month].plot(x='Order_Type', y='#_Days_@_Post_stream') plt.show()
the data chart is shown perfectly.
However when i am trying to put this into a subplot with the code below (only first two lines are added & ax=ax in the plot line), nothing shows up. I get no error either!!! can anyone help?
fig = plt.figure(figsize=(8, 6)) ax = fig.add_subplot(211, facecolor='#FFFFCC') input_month='201706' plt.close('all') KPI_ue_data.loc[input_month].plot(x='Order_Type', y='#_Days_@_Post_stream',ax=ax) plt.show()

Is there a way to interpolate series with recurring values using matplotlib?
I am trying to interpolate a x/y series using
matplotlib
. The problem I am facing is thatspline
andinterp1d
fail because I have recurring values in both the x and y arrays.I have tried using the spline and
interp1d
functions fromscipy
, but both fail because of the recurring values issuex1 = [0.82 0.82 0.82 0.82 0.82 0.82 0.83 0.83 0.83 0.83 0.83 0.83 0.83] y1 = [0.93 0.93 0.93 0.93 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.94] f = interp1d(x1, y1, kind='cubic') #this gives an error: Expect x to be a 1D sorted array_like. #another thing I tried xnew = np.linspace(x1.min(),x1.max(),300) splined = spline(x1,y1,xnew) #this gives an error: Matrix is singular
I am expecting the interpolated y value to gradually increase with the increase of x. So for example, the corresponding y value for x = 0.82 would be 0.931, 0.932, etc. My goal in the end is to get a smooth curve.

Implementing FloydWarshall in Java
I'm trying to implement the FloydWarshall algorithm and I'm a bit stuck on how to go about it.
I'm taking in an unspecified amount of weighted edges, the input is 3 integers: the source, destination and weight. Example:
1 9 4
1 is source node, 9 is destination node and 4 is weight.
Here is my code below:
import java.util.*; public class FloydWarshall { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); ArrayList<Edge> edges = new ArrayList<Edge>(); while(stdin.hasNext()) { String[] str = stdin.nextLine().split("[\\s,]+"); ArrayList<Integer> inner = new ArrayList<Integer>(); for(int i = 0; i < str.length; i++) { inner.add(Integer.parseInt(str[i])); } list.add(inner); } Graph graph = new Graph(edges.size()); for(int i = 0; i < list.size(); i++) { graph.addEdge(edges, list.get(i).get(0), list.get(i).get(1), list.get(i).get(2)); } } } class Edge { int source; int destination; int weight; public Edge(int source, int destination, int weight) { this.source = source; this.destination = destination; this.weight = weight; } } class Graph { int vertices; int adjMatrix[][]; public Graph(int vertices) { this.vertices = vertices; this.adjMatrix = new int[vertices][vertices]; } public void addEdge(ArrayList<Edge> edges, int source, int destination, int weight) { Edge edge = new Edge(source, destination, weight); edges.add(edge); adjMatrix[source][destination] = weight; } }
I'm trying to implement an adjacency matrix for the FloydWarshall algorithm but I'm having trouble implementing it, I've never used an adjacency matrix so any help would be appreciated!

Confused about stop condition on Spark/Graphx/Pregel example program to find 'path distance
'
I am working my way through Graphx In Action and this book (source code for which is here: https://github.com/insidedctm/sparkgraphxinaction) discusses two ways of calculating the distance (number of edge hops) between the root of a tree and all of the nodes down to the leaves. I understand the code examples that are provided using aggregateMessages. In particular, the stop condition makes sense ( I have highlighted that condition via the comment which includes the text 'STOP CONDITION', below.) Once the attributes on the vertices of the graph stop changing, it no longer makes sense to keep running the algorithm.
I was a little puzzled when I looked at the Pregel way of calculating the same result (shown below.)
In particular when Pregel's apply method is called, maxIterations is the default, Integer.MAX_VALUE (which is for all practical purposes 'run forever'.) Therefore, it seems like the 'sendMsg' function which is:
(et:EdgeTriplet[Int,String]) => Iterator((et.dstId, et.srcAttr+1)),
will be called infinitely, even after the values on the vertices have converged.
Is there some mechanism that I overlooked which causes the program to stop after convergence ?
// aggregateMessages approach // from: https://github.com/insidedctm/sparkgraphxinaction/blob/51e4c667b927466bd02a0a027ca36625b010e0d6/Chapter04/Listing4_10IteratedFurthestVertex.scala def sendMsg(ec: EdgeContext[Int,String,Int]): Unit = { ec.sendToDst(ec.srcAttr+1) } def mergeMsg(a: Int, b: Int): Int = { math.max(a,b) } def propagateEdgeCount(g:Graph[Int,String]) :Graph[Int,String] = { val verts = g.aggregateMessages[Int](sendMsg, mergeMsg) val g2 = Graph(verts, g.edges) val check = g2.vertices.join(g.vertices). map(x => x._2._1 – x._2._2). reduce(_ + _) // STOP CONDITION // check here ensures stop if nothing changed (******) if (check > 0) propagateEdgeCount(g2) else g } // Pregel approach val g = Pregel(myGraph.mapVertices((vid,vd) => 0), 0, activeDirection = EdgeDirection.Out)( (id:VertexId,vd:Int,a:Int) => math.max(vd,a), (et:EdgeTriplet[Int,String]) => Iterator((et.dstId, et.srcAttr+1)), (a:Int,b:Int) => math.max(a,b)) g.vertices.collect

Is there any way to add overlays to bokeh plot highlighting certain areas based on xaxis datapoints. See picture below
I am plotting some data on xaxis and yaxis which gives peaks. I want to highlight certain areas of the peaks in multiple colors.
I was able to get the plot but have no clue on how to add overlays to the plot
https://jascoinc.com/wpcontent/uploads/2013/12/UVfractioncollection.png

Padding Around Legend when using Pch in Base R
Just a minor question. I am trying to make a legend for the following plot.
# fitting the linear model iris_lm = lm(Petal.Length ~ Sepal.Length, data = iris) summary(iris_lm) # calculating the confidence interval for the fitted line preds = predict(iris_lm, newdata = data.frame(Sepal.Length = seq(4,8,0.1)), interval = "confidence") # making the initial plot par(family = "serif") plot(Petal.Length ~ Sepal.Length, data = iris, col = "darkgrey", family = "serif", las = 1, xlab = "Sepal Length", ylab = "Pedal Length") # shading in the confidence interval polygon( c(seq(8,4,0.1), seq(4,8,0.1)), # all of the necessary x values c(rev(preds[,3]), preds[,2]), # all of the necessary y values col = rgb(0.2745098, 0.5098039, 0.7058824, 0.4), # the color of the interval border = NA # turning off the border ) # adding the regression line abline(iris_lm, col = "SteelBlue") # adding a legend legend("bottomright", legend = c("Fitted Values", "Confidence Interval"), lty = c(1,0))
Here's the output so far:
My goal is to put a box in the legend next to the "Confidence Interval" tab, and color it in the same shade that it is in the picture. Naturally, I thought to use the
pch
parameter. However, when I rerun my code with the additional legend optionpch = c(NA, 25)
, I get the following:It is not super noticeable, but if you look closely at the padding on the left margin of the legend, it actually has decreased, and the edge of the border is now closer to the line than I would like. Is there any way to work around this?

How to reduce the legend symbol thickness for a bar chart in ggplot2
Was asked to reduce the legend symbol thickness for a bar chart in ggplot2 (need them so thin that they look like narrow horizontal lines). Here is a simplification of my case:
library(ggplot2) # Simple bar chart example g < ggplot(mpg, aes(class)) + geom_bar(aes(fill = drv)) g
# Failed attempt to reduce the thickness of the legend symbol using guides(). # I also tried negative values, but that gives errors. # However, increasing the size works well. I need the symbols very thin. g2 < g + guides(fill = guide_legend(override.aes = list(size = 0.1))) g2
# Also adjusting with some theme options is not really working for me # nor what I really need because is also reducing the distance between the labels. g + theme(legend.key.height = unit(0.1, "mm"))
Perhaps there is no other way around than editing the legend grobs themselves with the functionality of the
grid
package or do this outside of R, like Inkscape (?).^{Created on 20190521 by the reprex package (v0.2.1)}

D3 Scatterplot legend overlapping
I have a scatterplot that works fine, but the legend I add to it is overlapping the chart. My current approach is to make the chart DIV be 70% of the width and have the legend take up the remaining 30%. For some reason, the legend isn't showing up on the screen, even though the HTML is there.
This is the link to my initial problem: http://jsfiddle.net/chp5a09e/373/
Here is the link to what I'm currently trying: http://jsfiddle.net/chp5a09e/372/
var legend = d3.select("#legend").append("svg") .attr("width", $("#legend").width()) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); legend.selectAll(".legend") .data(color.domain()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width  12) .attr("width", 12) .attr("height", 12) .style("fill", function(d) { return color(d); }) .on("click", function(d) { d3.selectAll(".symbol").style("opacity", 1) if (clicked !== d) { d3.selectAll(".symbol") .filter(function(e) { return e.items[columns.indexOf("Channel")] !== d; }) .style("opacity", 0.1) clicked = d } else { clicked = "" } }); legend.append("text") .attr("x", width  16) .attr("y", 6) .attr("dy", ".35em") .style("textanchor", "end") .text(function(d) { return d; });

How do I reverse the odd indexed elements a list of lists?
The idea is to find a zig zag level order traversal of a tree. My approach is to find the level order traversal, and then reverse the elements which are at the odd number positions. However the test case submitted along with the expected output differ from my actual output.
# Definition for a binary tree node. class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def zigzagLevelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ #for all odd number indices, reverse the list ret = self.levelorder(root) print ret def levelorder(self,root): #first conduct a level order traversal #then check the level #if the level is odd, then reverse the elements in the list #otherwise, just print as is. returnlist = [] if not root: #check for null root return returnlist q = [(root,0)] if root: while q: node, level = q.pop(0) # store node object and level if len(returnlist) < level + 1: returnlist.append([node.val]) else: returnlist[level].append(node.val) if level % 2 != 0: returnlist[level].reverse() if node.left: q.append((node.left, level + 1)) if node.right: q.append((node.right, level+1)) return returnlist
testcase =
[0,2,4,1,None,3,1,5,1,None,6,None,8,20,None,3,5,None,6,7,None,4,5,6,7]
stdout
[[0], [4, 2], [1, 3, 1], [8, 1, 5, 6], [20, 3, 5, 6, 7], [7, 5, 4, 6]]
Expected
[[0],[4,2],[1,3,1],[8,6,1,5],[20,3,5,6,7],[7,6,5,4]]

Reverse only numbers from file
How can I reverse only number and not the text from this ?
datas.txt
Bungo Charlie Bungo Echo Bungo Bravo Bungo Tango Bungo Alpha
with open('datas.txt', 'r') as f: for i, line in enumerate(f): print('{}. {}'.format(i+1, line.strip()))
Expectation:
5. Bungo Charlie 4. Bungo Echo 3. Bungo Bravo 2. Bungo Tango 1. Bungo Alpha
What I got :
1. Bungo Charlie 2. Bungo Echo 3. Bungo Bravo 4. Bungo Tango 5. Bungo Alpha

Reverse Array, Let Elements in New Array Equal Length of Original Array Elements  JavaScript
I'm ultimately trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but I don't know how to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array) { let newArray = array.join("").split(""); let reversedArray = newArray.reverse(); return reversedArray.join(""); } console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"])); //!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on.
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array) { let elementLengths = []; let newArray = array.join("").split(""); let reversedArray = newArray.reverse().join(""); for (let i = 0; i < array.length; i++) { let element = array[i]; elementLengths.push(element.length); } return reversedArray + " " + elementLengths; } console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"])); //!eiltonnacIdnasttubgibekilI 1,4,3,5,3,1,6,4
Now if I can just split the string into elements in an array based on the length of the original elements...