openpyxl conditional formatting with named style - python

I have a named style, defined with:
style = openpyxl.styles.NamedStyle('style1')
style.fill = openpyxl.styles.PatternFill(patternType='solid', fill_type='solid', fgColor='BB0000')
style.border = openpyxl.styles.Border(left=openpyxl.styles.borders.Side(style='thin'),
right=openpyxl.styles.borders.Side(style='thin'),
bottom=openpyxl.styles.borders.Side(style='thin'),
top=openpyxl.styles.borders.Side(style='thin') )
I want to have a conditional format, based on cell value, that uses this style.
I can do something like:
rule = openpyxl.formatting.rules.CellIsRule(operator='lessThan', formula=['0']', ...)
But this doesn't seem to take a named style. It takes either a differential style, or a fill and a border as separate things...

You can't use named styles with conditional formats. There are subtle differences in the way they're applied but they are also implemented and stored differently. Just create a DifferentialStyle in the same way as a named style and set this as the dxf for the rule. You can probably do this very easily:
dxf = DifferentialStyle(**dict(style))

Related

Python folium - Markercluster not iterable with GroupedLayerControl

I would like to group my 2 marker cluster layers, where one is reliant on the other by providing a separate styling. Hence the second one is set as control=False.
Nevertheless, I want to have it disappear when the first one is switched off.
Along with the new Python folium issue v.0.14 I found, that the new feature has been provided, which potentially could resolve my issue:
https://github.com/ikoojoshi/Folium-GroupedLayerControl
Allow only one layer at a time in Folium LayerControl
and I've applied the following code:
df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="OR")
or_cluster = MarkerCluster(name="Or", overlay=True, visible=True).add_to(map)
or_status = MarkerCluster(overlay=True,
control=False,
visible=False,
disableClusteringAtZoom=16,
).add_to(map)
GroupedLayerControl(
groups={'OrB': or_cluster, 'OrC': or_status},
collapsed=False,
).add_to(map)
and the console throws the following error:
TypeError: 'MarkerCluster' object is not iterable
How could I switch off 2 layer groups at once?
UPDATE:
The answer below provides the code, which seems to work but not in the way I need.
df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="Or",overlay = True)
or_cluster = MarkerCluster(name="Or").add_to(map)
or_status = MarkerCluster(control=False,
visible=True,
disableClusteringAtZoom=16,
).add_to(map)
# definition of or_marker
# definition of or_stat_marker
or_cluster.add_child(or_marker)
or_status.add_child(or_stat_marker)
GroupedLayerControl(
groups={"Or": [or_cluster, or_status]},
collapsed=False,
exclusive_group=False,
).add_to(map)
I have a separate box instead, but what is worst I can just switch between one layer and another whereas I would like to have them reliant on the main group. The exclusive_groups option allows me to untick both of them but I am looking for something, which would allow me to switch off two of them at once (place the thick box on the major group instead). Is it possible to have something like this?
Try passing your markerclusters as a list to the GroupedLayerControl, not one by one. This is described here:
https://nbviewer.org/github/chansooligans/folium/blob/plugins-groupedlayercontrol/examples/plugin-GroupedLayerControl.ipynb
GroupedLayerControl(
groups={'OrB': [or_cluster, or_status]},
collapsed=False,
).add_to(map)
Update I
I see what you mean, that was definitely nonsense as it splits groups instead of joining them. so, back to topic
We had a similar discussion here and I am still convinced that the FeatureSubGroup should solve this issue. I use it in exact that way that I enable/disable a MarkerCluster in the legend and multiple FeatureGroupSubGroups (which are added not to the map but to the MarkerCluster) appear/disappear. Perhaps you try that again

How to use named selection for filtering in Vaex

I created 2 named selections
df.select(df.x => 2,name='bigger')
df.select(df.x < 2,name='smaller')
and it's cool, I can use the selection parameter so many (ie statistical) functions offer, for example
df.count('*',selection='bigger')
but is there also a way to use the named selection in a filter? Something like
df['bigger']
Well that syntax df['bigger'] is accessing a column (or expression) in vaex that is called 'bigger'.
However, you can do: df.filter('bigger') and will give you a filtered dataframe.
Note that, while similar in some ways, filters and selections are a bit different, and each has its own place when using vaex.

use custom functions in transform_calculate in altair

i have a bar chart, that i want to add approximation of each bar value, above each itself.
that value is a big number, i have a function that formats number e.g.: 10,000$ to 10k$. how can i apply that.
base = alt.Chart(target_df_hahu).mark_bar().encode(
alt.X('monthdate(date):O'),
alt.Y('value'),
color = 'variable'
)
i have try blow code .
text = base.mark_text().encode(
text = 'value:Q'
).transform_calculate(
value=custom_function(datum.value)
)
base+text
Calculate transforms are evaluated in Javascript by the Vega renderer, and thus cannot contain custom functionality defined in Python. Calculate transforms must be strings containing a well-defined subset of javascript syntax, making use of any of the functionality listed at https://vega.github.io/vega/docs/expressions/

openpyxl - Changing the scatterStyle of a chart to 'marker'

I use openpyxl to create scatterCharts in an .xlsx-file.
The default style of the chart is "line". I want to change this style to "marker".
http://openpyxl.readthedocs.io/en/default/charts/scatter.html
says, that it is the best way to do this by changing the style of the series.
I tried different things:
1st:
chart = openpyxl.chart.ScatterChart(scatterStyle='marker')
--> no Effect
2nd:
chart.scatterStyle = "marker"
--> no Effect, maby i have to place this line on a special place?
3rd:
series = openpyxl.chart.Series(yvalues, xvalues, title_from_data=True)
series.marker=openpyxl.chart.marker.Marker('x')
--> now i have lines with markers, so it seems, that i am on the right way. But i have not found a way to remove the lines.
I found a solution:
series.marker=openpyxl.chart.marker.Marker('x')
series.graphicalProperties.line.noFill=True
will add markers to the graph and remove the lines.
Additional information:
to find properties and methods of the objects like "series" you can use
dir(series)
to show all properties and methods of this object. There you find the "graphicalProperties"
and with
dir(series.graphicalProperties)
you can find "line" ... and so on

Using two different text sizes in one legend in Python

I am using matplotlib
I have a legend on a graph in Python which is:
plt.text(sigma1+5,5,str("$\sigma$("+"%.2f"%(sigma1) + ",0)"),color='red')
I'd like the symbol sigma to be larger than the rest of the text. Is this possible? Or do I have to create two separate legends?
Unfortunately, a matplotlib.text.Text instance uses a single style (font, size, etc.) for the whole string. So yes, I'm pretty sure you're going to need to create two of them.
If you don't know how to set the font size, see the docs for matplotlib.pyplot.text: you can either pass an optional fontdict argument that specifies font properties, or you can pass extra keyword arguments like size or fontproperties that get passed on to the Text constructor.

Categories