I am unable to change the default circle marker size in folium.
This is what my code looks like:
import folium
import pandas as pd
data = pd.read_csv("Volcanoes_USA.txt")
map = folium.Map(location=[46,-120],zoom_start=5)
fg = folium.FeatureGroup(name="My Map")
def colorcode(x):
if x in range(0,1600):
color = 'green'
elif x in range(1600,2200):
color = 'orange'
elif x in range(2200,2800):
color = 'red'
else:
color = 'darkred'
return color
lat = list(data["LAT"])
long = list(data["LON"])
elev = list(data["ELEV"])
for lt,ln,el in zip(lat,long,elev):
fg.add_child(folium.CircleMarker(location = [lt,ln],
radius = 10,
color = 'black',
fill_color = colorcode(int(el)),
fill_opacity = 0.6,
popup = "Elevation: %s meters" %el))
map.add_child(fg)
map.save("Volcanoes1.html")
And this is how my output looks no matter whatever radius value I use.
Can someone please help?
Regards
Updating the folium version to 0.5.0 fixed it. If you are using anaconda like I was, here's the command to update it:
conda update -c conda-forge folium
Related
I'm making a graph in Shiny using shinyCyJS, I'm building the edges with buildElems that calls buildEdge:
edges = data.frame(
source = c("v1","v2","v3","v4","v4"),
target = c("v2","v3","v4","v2","v1"),targetArrowShape ='triangle',targetArrowColor = "#000000"
)
edges = buildElems(edges, type = 'Edge')
But the parameters aren't working, I want to:
Build a simple arrow from source to target.
Make the edge black.
Write a legend above the edge.
How can I do those? Thanks.
This works:
edges = data.frame(
source = c("v1","v1","v2","v3","v3"),
target = c("v2","v3","v4","v2","v4"),
label = c("7","3","2","2","8"),
lineColor = "#9b9b9b",
curveStyle='bezier',
targetArrowColor = 'black',
sourceArrowColor = 'black',
targetArrowShape ='triangle',
sourceArrowShape = 'none'
)
I see how to customize the appearance of a hovered point in a Bokeh plot, for example with something like
p.add_glyph(ColumnDataSource(source), initial_circle, hover_glyph=hovered_circle, ...)
but can't see to find anything about customizing the un-hovered points when a point is hovered.
Is there a way to specify how un-hovered points should appear, distinct from their default ("initial") appearance, analogous to nonselection_glyph vs. selection_glyph?
The Bokeh GlyphRenderer documentation doesn't specifies anything like this. The easy alternative is to create your own HoverTool callback to give the un-hovered circles your desired appearance like in this example (working for Bokeh v1.0.4)
from bokeh.models import ColumnDataSource, HoverTool, CustomJS
from bokeh.plotting import show, figure
source = ColumnDataSource(dict(x = [1, 2], y = [3, 4], color = ['blue', 'blue']))
p = figure(tools = 'hover')
c = p.circle(x = 'x', y = 'y', color = 'color', size = 20, source = source)
code = ''' if (cb_data.index.indices.length > 0){
selected_index = cb_data.index.indices[0];
for (index in source.data['color']){
if (index == selected_index)
source.data['color'][index] = 'red';
else
source.data['color'][index] = 'yellow';
source.change.emit();
}
else{
for (index in source.data['color'])
source.data['color'][index] = 'blue';
} '''
p.select(HoverTool).callback = CustomJS(args = dict(source = source), code = code)
show(p)
Result;
I would like to change the data source for bokeh network graph. However the from_network function does not allow me to input source = new_source for example. However do I change the data source for the network graph to be updated if click a button?
#PLOT GRAPH WITH BOKEH SERVER
def update_plot_layout(layout_name, range, source_bar):
plot_degree = figure(title='Knowledge Graph', x_range=range, y_range=range,
tools=['box_select,pan,wheel_zoom,box_zoom,reset'],
toolbar_location="right",
toolbar_sticky=False)
plot_degree.grid.grid_line_color = None
graph = from_networkx(g, layout_name , scale=2, center=(0,0))
# graph.node_renderer.data_source = source
#
graph.node_renderer.glyph = Circle(size=20,
fill_color= 'crimson',
fill_alpha = 0.8, line_color = 'black')
# graph.node_renderer.glyph = Circle(size='node_size',
# fill_color= 'crimson',
# fill_alpha = 0.8, line_color = 'black')
#
graph.edge_renderer.glyph = MultiLine(line_color='gray', line_width=1.5, line_alpha = 0.5)
# plot_degree.add_tools(HoverTool(line_policy='interp', tooltips=[('ID', '#index'),
# ('Degree Centrallity', '#degree_centrality')]))
plot_degree.renderers.append(graph)
I am using magick library in R. I want to add watermark on some pictures.
I used image_annotatefunction as below.
img <- image_read("C:\\Users\\Maydin\\Desktop\\manzara.png")
image_annotate(img, "my watermark", gravity = "northwest", location = "+200+275",
degrees = -30, size =50, font = NULL, color = "transparent",
strokecolor = "gray90", boxcolor = NULL)
At the end, the output looks like this;
However, what I want to have is something like this ,
Is that doable in magick in R?
For instance, this
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
library(magick)
img <- image_read(tf)
library(extrafont)
truetype_path <- paste0("#", subset(fonttable(), FullName=="Matura MT Script Capitals", fontfile)[1,])
image_annotate(img, "my watermark", gravity = "northwest", location = "+70+220",
degrees = -30, size = 80, font = truetype_path, color = "#FFFFFF66",
strokecolor = NULL, boxcolor = NULL)
gives this image:
I.e., choose a nice font like maybe Matura MT Script Capitals, tell image_annotate where to find it on your harddrive, adjust the opacity in the color argument - et voila. The font does not drop a shadow or show a relief, but maybe you can emulate this by plotting the text two times, the dark shadow one with a little offset to the other light one.
#lukA nicely demonstrates a solution using extrafonts package with magick package, but it looks like you can refer to the font by name within image_annotate() without the clever lookup of the full path. Use extrafonts::fonttable() to find the name.
library(extrafonts)
library(magick)
#download original example file
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
img <- image_read(tf)
#Use stroke to create an outline of the text with 50% alpha
magick::image_annotate(img, "Preview", location = "+100+175", degrees = -30, size=75, weight=700, font = "MonotypeCorsiva" , color = "transparent",
strokecolor = "#00000050", boxcolor = NULL)
I am attempting to get a matplotlib plotting function to be able to produce a graph with the x-axis set as a time axis. However, when I attempt to plot some values against UNIX times, I encounter the error ValueError: year is out of range. What is going wrong and how can it be addressed?
import os
import time
import matplotlib.dates
import matplotlib.pyplot
import shijian
def main():
data = [
[1484611200.0, 844.4333],
[1484524800.0, 783.3373],
[1484438400.0, 774.194 ],
[1484352000.0, 769.2299]
]
save_graph_matplotlib(
values = data,
line = True,
line_width = 0.5,
title_axis_x = "time",
title_axis_y = "value",
#time_axis_x = True
)
def save_graph_matplotlib(
values = None,
title = None,
title_axis_x = None,
title_axis_y = None,
filename = None,
directory = ".",
overwrite = True,
color = "black",
LaTeX = False,
markers = True,
marker_size = 1,
aspect = None,
line = False,
line_style = "-",
line_width = 0.2,
font_size = 20,
scientific_notation = False,
time_axis_x = False
):
# 1D or 2D data
if isinstance(values[0], list):
x = [element[0] for element in values]
y = [element[1] for element in values]
else:
x = range(0, len(values))
y = values
matplotlib.pyplot.ioff()
if LaTeX is True:
matplotlib.pyplot.rc("text", usetex = True)
matplotlib.pyplot.rc("font", family = "serif")
if filename is None:
if title is None:
filename = "graph.png"
else:
filename = shijian.propose_filename(
filename = title + ".png",
overwrite = overwrite
)
else:
filename = shijian.propose_filename(
filename = filename,
overwrite = overwrite
)
figure = matplotlib.pyplot.figure()
if title is not None:
figure.suptitle(
title,
fontsize = font_size
)
if markers is True:
matplotlib.pyplot.scatter(
x,
y,
s = marker_size,
c = color,
edgecolors = "none",
)
if line is True:
matplotlib.pyplot.plot(
x,
y,
line_style,
c = color,
linewidth = line_width
)
# Turn on or off axes scientific notation.
if scientific_notation is False:
matplotlib.pyplot.gca().get_xaxis().\
get_major_formatter().set_scientific(False)
matplotlib.pyplot.gca().get_yaxis().\
get_major_formatter().set_scientific(False)
# Set axes titles.
if title_axis_x is not None:
matplotlib.pyplot.xlabel(title_axis_x, fontsize = font_size)
if title_axis_y is not None:
matplotlib.pyplot.ylabel(title_axis_y, fontsize = font_size)
# Set axes font size.
matplotlib.pyplot.xticks(fontsize = font_size)
matplotlib.pyplot.yticks(fontsize = font_size)
# Set or do not set axis x as time.
if time_axis_x:
time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
matplotlib.pyplot.axes().xaxis_date()
matplotlib.pyplot.axes().xaxis.set_major_formatter(time_formatter)
matplotlib.pyplot.xticks(rotation = -90)
# Set the aspect ratio.
if aspect is None:
matplotlib.pyplot.axes().set_aspect(
1 / matplotlib.pyplot.axes().get_data_ratio()
)
else:
matplotlib.pyplot.axes().set_aspect(aspect)
if not os.path.exists(directory):
os.makedirs(directory)
matplotlib.pyplot.savefig(
directory + "/" + filename,
dpi = 700
)
matplotlib.pyplot.close()
if __name__ == "__main__":
main()
You need to convert your timestamp-like x data to a python datetime object, which can then be used in matplotlib and be understood by the matplotlib.dates.DateFormatter.
This can be done using the datetime.datetime.fromtimestamp() method.
import datetime
import matplotlib.dates
import matplotlib.pyplot as plt
data = [
[1484611200.0, 844.4333],
[1484524800.0, 783.3373],
[1484438400.0, 774.194 ],
[1484352000.0, 769.2299]
]
x = [datetime.datetime.fromtimestamp(element[0]) for element in data]
y = [element[1] for element in data]
plt.plot( x, y, ls="-", c= "b", linewidth = 2 )
plt.xlabel("Dates")
time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
plt.axes().xaxis.set_major_formatter(time_formatter)
plt.axes().xaxis_date() # this is not actually necessary
plt.show()
Whilst not directly addressing the text of the question, the error mentioned in the title can also occur when one attempts to plot data on an existing axis whose timeline units don't match those of the plot data (e.g. seconds vs datetime).