Flot bar chart highlight color per series - flot

I have a bar chart with a few series. How can I control what color the highlight color will be on a per series basis? (each of my series is a different colour so I need to highlight them each differently).
I've seen how to do this for a single color throughout the entire chart but I'm needing it specific for each series.

You need to set the highlightColor option for each series in your data array:
var data = [{
data: [
[1, 5],
[2, 3]
],
highlightColor: "#00FF00"
}, {
data: [
[1, 2],
[2, 8]
],
highlightColor: "#FF0000",
}];
JSFiddle Example

Related

Hi I need some help on understanding how the transposed code works [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm not very good at coding so any help in understanding this code would be appreciated. I don't understand how the code works. I think I have a general idea of how it works. I think the variable "I" is somehow assigned to each one of the numbers in the matrix and the row is constantly switching between the 3 to create the groups. The thing is I don't think that this is correct because I don't see the variable "I" assigned to any of the matrix columns neither do I see rows being assigned to the 3 rows from the matrix. Any help would be appreciated in understanding this piece of code. In addition, I am a bit dumb so if you could explain in a very simple manner it would be very helpful. Thank you.
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
transposed = []
for i in range(4):
# the following 3 lines implement the nested listcomp
transposed_row = []
for row in matrix:
transposed_row.append(row[i])
transposed.append(transposed_row)
transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
If you look at the transposed result you can see that each row (sub-list) represents one column from the original matrix where a column is defined as elements sharing a common row (sub-list) index:
Value of transposed list and assocaited sub-lists:
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
The first column (index 0) from matrix highlighted in red:
So, the actual code that performs the transposition is essentially going column by column:
for i in range(4):
And for each column iterating over each row:
for row in matrix:
And building a new row from each element in the original matrix row at the column ("i-th") position:
transposed_row.append(row[i])
When the new transposed row is complete (after all original rows have been iterated over) for the current column, the new row is appended to the transposed list and the process starts all over again with the next column (next value of i):
transposed.append(transposed_row)

Way to see the missing columns after calculating the minimum value in Pandas

Using pandas, I grouped a dataset by window number(winnum), latitude, and longitude.
The code is as follows.
final=[(win[j],ttdf[0][i],ttdf[1][i],(ttdf[2][i]-shift[j])**2) for i in range(len(ttdf))
for j in range(len(ccdf))]
fidf=pd.DataFrame(final)
winnum=fidf[0]
latitue=fidf[1]
longitude=fidf[2]
difference=fidf[3]
titles = {0: 'winnum', 1: 'latitude', 2: 'longitude', 3: 'difference'}
fidf.rename(columns=titles, inplace=True)
Then I summed the difference value of each group to find the minimum value for each set of latitude and longitude.
grouped=fidf['difference'].groupby([fidf['winnum'],fidf['latitude'],fidf['longitude']])
s=grouped.sum()
lastdf=pd.DataFrame(s)
lastdf.min(level='winnum')
However, if I type the code above, I can only see two columns, which are 'winnum' and the minimum value of the sum of 'difference'.
What I want to do is to check the value of (latitude,longitude) which has the minimum value of 'difference sum' for each winnum.
Is there any way that I can see latitude and longitude columun here even after I calculate the minimum value of difference sum?
It would be a great help for me if you give me the answer. Thanks :)
Your groupby should result in Series with the sum of the differences. The winnum, latitude and longitude are still present in the index though.
Example:
fidf = pd.DataFrame({'winnum': [0,0,1,2],
'latitude': [1, 1, 2, 2],
'longitude': [3, 3, 4, 5],
'difference': [1, 2, 3, 4]})
grouped = fidf['difference'].groupby([fidf.winnum,
fidf.latitude,
fidf.longitude]).sum()
print(grouped.index.names)
# ['winnum', 'latitude', 'longitude']
You can get the index values for the minimum sum of differences with idxmin
winmum, lat, long = grouped.idxmin()
#(0, 1, 3)
If you want the row for each winnum with the minimum sum of difference, you can use the following lookup:
grouped.loc[grouped.groupby('winnum').idxmin()]
There's definitely a smarter way of keeping the value - however, why not simply add it back? Once you have lastdf:
lastdf = lastdf.reset_index()
lastdf.merge(fidf,how='left',on=['winnum','difference'])
This should just take the lat and lon of lines in fidf with the same winnum and difference and add it on.

Replace indicator values with actual values

I have a numpy array like this
array([[0, 0, 1],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
and an array with values
array([1, 2, 3, 4])
I would like to replace the ones in the first two-dimensional array with the corresponding values in the second array. Each row of the first array has exactly one 1, and there is only 1 replacement in the second array.
Result:
array([[0, 0, 1],
[2, 0, 0],
[0, 3, 0],
[0, 0, 4]])
I would like an elegant solution to achieve this, without loops and such.
Let's say a is the 2D data array and b the second 1D array.
An elegant solution would be -
a[a==1] = b
For performance, leveraging the fact that there's exactly one 1 per row, we could also use indexing -
a[np.arange(len(a)),a.argmax(1)] = b
Selectively assign per row
If we want to selectively mask and asign values per row, we could use one more level of masking. So, let's say we have the rows to be selected as -
select_rows = np.array([1,3])
Then, we could do -
rowmask = np.isin(np.arange(len(a)),select_rows)
So, for the replacement for the first approach would be -
a[(a==1) & rowmask[:,None]] = b[rowmask]
And for the second one -
a[np.arange(len(a))[rowmask],a.argmax(1)[rowmask]] = b[rowmask]

Calculate the percent change between every rolling nth row in a Pandas DataFrame

How can I calculate the percentage change between every rolling nth row in a Pandas DataFrame? Using every 2nd row as an example:
Given the following Dataframe:
>df = pd.DataFrame({"A":[14, 4, 5, 4, 1, 55],
"B":[5, 2, 54, 3, 2, 32],
"C":[20, 20, 7, 21, 8, 5],
"D":[14, 3, 6, 2, 6, 4]})
I would like the resulting DataFrame to be:
But, the closest I am getting by using this code:
>df.iloc[::2,:].pct_change(-1)
Which results in this:
It is performing the calculation for every other row but this is not the same as the a rolling window of calculating every nth row. I came across a similar Stack post but that example is not very straightforward.
Also, as a bonus, I'd like to display the resulting output as a percentage to two decimal places.
Thank you for your time!
Got it! Use the option "periods" for 'pct_change()'.
>df.pct_change(periods=-n) #where n=2 for the given example.

Is there a way to color values in a dataframe if the belong to a certain range (Python-Pandas)

I have a data frame with values from 0 to 10. I would like to color the value 1 and 5 with red rather than black. Is that possible to do it in python DataFrame? I am using Jupyter notebook.
You can change the style of cells -
df = pd.DataFrame({'v1': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})
dft = df.style.applymap(lambda x: 'color: red' if x >= 1 and x <=5 else 'color: black')
dft
You can find more information about applying styles here - http://pandas.pydata.org/pandas-docs/stable/style.html

Resources