How to manually set a diverging color range - altair

I am using Altair for Python and my current code uses a redyellowblue color scheme which uses the middle color (yellow) accordingly to my domainMid parameter.
color=alt.Color('Spline_WW_Diff_Trend:Q', scale=alt.Scale(scheme='redyellowblue',reverse=True, domain=[-3.57,2.270], domainMid=0, clamp=True), legend=alt.Legend(title="Trend"))
Which gives me:
I need to replace the yellow color with the white color. I've switched scheme to range and tried different combinations of Scale parameters, but it doesn't respect my domainMid.
Example:
color=alt.Color('Spline_WW_Diff_Trend:Q', scale=alt.Scale(range=['#D4322C', 'white', '#4A74B4'],reverse=True, domain=[-3.57,2.270], domainMid=0, clamp=True), legend=alt.Legend(title="Trend"))
This gives me:
You can notice that the first column (which is all value 0) is showing reddish and not white (as it was supposed to be).
How can I have the same result as in the color scheme (picture one), but, with white instead of yellow?
Edit:
The same goes for.
range=['blue', 'lightblue', 'white', 'pink', 'red']
The mid color is light-blue, not white.
Edit 05/12/2022:
Just to clarify, I would like to achieve this same color scheme:
.
This heatmap was created in R, by using the RdYlBu color pallete (the one with 11 colors) and overwrite the middle (6th color) with white. Then, they increased the number of colors in the pallete to 99, to make the fade look more fluid. Does anyone has any idea on how to achieve that in Altair?

The easiest approach would be to set range='diverging', or to use one of the built-in diverging color schemes. For example:
import altair as alt
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': np.arange(-10, 20)})
alt.Chart(df).mark_rect().encode(
x='x:O',
color=alt.Color('x:Q',
scale=alt.Scale(domainMid=0, scheme='redblue'))
)
If it's important to you that the center value is exactly white, you could use a condition to override the colormap for this value. For example:
alt.Chart(df).mark_rect().encode(
x='x:O',
color=alt.condition(
'datum.x == 0',
alt.value('white'),
alt.Color('x:Q', scale=alt.Scale(domainMid=0, scheme='redblue')))
)

Related

Openpyxl: Decode 'auto' color to rgb value

I am trying to determine the color of borders set in an Excel document. I use the 'openpyxl' library (latest Version 3.0.9) and encountered a problem to extract the rgb color code when the user sets the border via the default 'auto' color.
When I extract the cell color property, I see this (in my debugger, see screenshot). None of the cell fields are set with an RGB code, the fields indicate some problem with extracting the color information. I assume this is due to the 'auto' color.
The index color probably refers to this in documentation:
Default Color Index as per 18.8.27 of ECMA Part 4
COLOR_INDEX = (
'00000000', '00FFFFFF', '00FF0000', '0000FF00', '000000FF', #0-4
'00FFFF00', '00FF00FF', '0000FFFF', '00000000', '00FFFFFF', #5-9
'00FF0000', '0000FF00', '000000FF', '00FFFF00', '00FF00FF', #10-14
'0000FFFF', '00800000', '00008000', '00000080', '00808000', #15-19
'00800080', '00008080', '00C0C0C0', '00808080', '009999FF', #20-24
'00993366', '00FFFFCC', '00CCFFFF', '00660066', '00FF8080', #25-29
'000066CC', '00CCCCFF', '00000080', '00FF00FF', '00FFFF00', #30-34
'0000FFFF', '00800080', '00800000', '00008080', '000000FF', #35-39
'0000CCFF', '00CCFFFF', '00CCFFCC', '00FFFF99', '0099CCFF', #40-44
'00FF99CC', '00CC99FF', '00FFCC99', '003366FF', '0033CCCC', #45-49
'0099CC00', '00FFCC00', '00FF9900', '00FF6600', '00666699', #50-54
'00969696', '00003366', '00339966', '00003300', '00333300', #55-59
'00993300', '00993366', '00333399', '00333333', #60-63
)
# indices 64 and 65 are reserved for the system foreground and background colours respectively
The documentation says that Index 64 is reserved so there is no point in trying to access the color array (max index is 63).
Source: https://openpyxl.readthedocs.io/en/stable/_modules/openpyxl/styles/colors.html
I can be brave and deal with index 64 as black, which seems to be the eternal auto-color anyway but I am wondering if there is a proper way to decode the 'auto' color from Excel?
System colours are set by the application. Windows defaults will be white background and black foreground. You can check the colors part of the styles.xml to see if these have been overriden, but really the client application gets to decide.

Is there any way to give gradient color to normal color property in flutter?

I am using Bar Chart of fl_chart plugin. I want to give gradient color to bar graph. Below is the line where I want give color.
BarChartRodData(y: 12, color: Color(0xFF639ed1),width: 12)
But it is not possible to set gradient color to the ##color## property unless it is colors.
Is there any way to set gradient color to color property?? Please help me..
If you pass just one color, the solid color will be used, or if you pass more than one color, we use gradient mode to draw. then the gradientFrom, gradientTo and gradientColorStops
For an example ,
final List<Color> color = <Color>[];
color.add(Colors.blue[50]);
color.add(Colors.blue[100]);
color.add(Colors.blue);
final List<double> stops = <double>[];
stops.add(0.0);
stops.add(0.5);
stops.add(1.0);
Please refer https://pub.dev/documentation/fl_chart/latest/fl_chart/BarChartRodData-class.html for BarChartRodData class variables

Altair color bar chart by value not presented

Trying to color a bar chart using a condition based on a value that is not presented in the chart.
I got this dataframe:
I would like to color the bar green if row.presented_value > row.coloring_value , else color red.
I saw examples of conditions by constant values and by displayed values, but couldn't make it work for me.
In the code example below I would like both foo and bar to be red.
import pandas as pd
df = pd.DataFrame({'name':['bar','foo'],
'presented_value':[10,20],
'coloring_value':[15,25]})
(alt.Chart(df, height=250, width=375).mark_bar()
.encode(x='name', y=alt.Y('presented_value', axis=alt.Axis(orient='right')),
color=alt.condition(alt.datum['presented_value'] > df.loc[df.name==alt.datum.x,
'coloring_value'].values[0],
alt.value('lightgreen'),alt.value('darkred'))
)
)
Changing the first value of coloring_value to <10 both bars will be green even though I would expect only bar to be green.
df = pd.DataFrame({'name':['bar','foo'],
'presented_value':[10,20],
'coloring_value':[5,25]})
(alt.Chart(df, height=250, width=375).mark_bar()
.encode(x='name', y=alt.Y('presented_value', axis=alt.Axis(orient='right')),
color=alt.condition(alt.datum['presented_value'] > df.loc[df.name==alt.datum.x,
'coloring_value'].values[0],
alt.value('lightgreen'),alt.value('darkred'))))
Still not coloring by the correct values. Any idea on how to get it done?
Thanks in advance!
Condition expressions cannot use pandas constructs; they must map to vega expressions. Altair provides the alt.datum and alt.expr object as convenience wrappers for this.
In your case, when you want to compare two values in the row, the best way to do that is to compare them directly:
(alt.Chart(df, height=250, width=375).mark_bar()
.encode(
x='name',
y=alt.Y('presented_value', axis=alt.Axis(orient='right')),
color=alt.condition(
alt.datum.presented_value > alt.datum.coloring_value,
alt.value('lightgreen'),
alt.value('darkred')
)
)
)

How to use df.plot to set different colors in one plot for one line?

I need to plot line plot that has different colors. I create special df column 'color' that contains for each point appropriate color.
I already found the solution here:
python/matplotlib - multicolor line
And take the approach from the above question. First, it was working when I use index but now I need to plot it vs other column and I can not appropriately handle the colors. It is all the time colores only with one color.
I use this code for setting colors, but it color line with one color that is the last in the column 'color'. And also create a legend that I don't understand how to delete from the plot.
for color2, start, end in gen_repeating(df2['color']):
print(start, end)
if start > 0: # make sure lines connect
start -= 1
idx = df2.index[start:end+1]
x2 = idx
y2 = df2.loc[idx, 'age_gps_data'].tolist()
df2.plot(x='river_km', y='age_gps_data', color=color2, ax=ax[1])
ax[1].xaxis.set_major_locator(plt.MaxNLocator(5))
plt.setp(ax[1].get_xticklabels())
I would appreciate any help.
How can I set these colors to achieve different color in one line? And don't have legend on the plot.

d3.js color range and lighter color range

I would like to have two color ranges, the second one must consist off the same colors, but lighter.
rangeLength=10
color = d3.scale.linear().domain([1,rangeLength]).range(['red', 'blue']);
colorLigher= d3.scale.linear().domain([1,rangeLength]).range(['red'.lighter(10), 'blue'.lighter(10)]);
Obviously doesn't work, as 'red' is a string.
Thanks in advance
You can use the brighter() function (see the documentation):
length=10
color = d3.scale.linear().domain([1,length]).range(['red', 'blue']);
colorLighter= d3.scale.linear().domain([1,length])
.range([d3.rgb('red').brighter(), d3.rgb('blue').brighter()]);

Resources