I have a weighted undirect graph as an igraph object :
IGRAPH 7d6665b UNW- 168 2345 --
+ attr: name (v/c), label (v/c), degree_alpha (v/n)
+ edges from 7d6665b (vertex names):
[1] 7 --13 7 --15 13--15 11--16 15--17 6 --18 15--20 6 --25 18--25 6 --28 10--28 15--28 18--28 20--28 25--28 23--30 15--31
[18] 17--31 28--31 6 --33 17--33 18--33 25--33 28--33 7 --34 13--34 15--34 16--35 15--37 18--37 20--37 25--37 28--37 13--43
+ ... omitted several edges
I use the function ggnet2 (part of GGally) to plot my igraph object as a ggplot2 object :
ggnet2(graph,
node.size = V(graph)$degree_alpha,
edge.label = NULL,
edge.size = E(graph)$weight/3,
node.label = NULL,
color = "#038e9a",
alpha = 0.75,
edge.color = "#6E6E6E",
legend.size = 0) + guides(color = FALSE, size = FALSE)
When I export to PNG, or just the preview given by RStudio, it looks like this :
When I export to PDF, I have a different output, with thicker edges :
You can find the graph object in RDS format here. How can I make my PDF export looks the same as the PNG one ? What does explain the difference in the first place ?
Related
I would like to plot a two variable function(s) (e_pos and e_neg in the code). Here, t and a are constants which I have given the value of 1.
My code to plot this function is the following:
t = 1
a = 1
kx = ky = range(3.14/a, step=0.1, 3.14/a)
# Doing a meshgrid for values of k
KX, KY = kx'.*ones(size(kx)[1]), ky'.*ones(size(ky)[1])
e_pos = +t.*sqrt.((3 .+ (4).*cos.((3)*KX*a/2).*cos.(sqrt(3).*KY.*a/2) .+ (2).*cos.(sqrt(3).*KY.*a)));
e_neg = -t.*sqrt.((3 .+ (4).*cos.((3)*KX*a/2).*cos.(sqrt(3).*KY.*a/2) .+ (2).*cos.(sqrt(3).*KY.*a)));
using Plots
plot(KX,KY,e_pos, st=:surface,cmap="inferno")
If I use Plots this way, sometimes I get an empty 3D plane without the surface. What am I doing wrong? I think it may have to do with the meshgrids I did for kx and ky, but I am unsure.
Edit: I also get the following error:
I changed some few things in my code.
First, I left the variables as ranges. Second, I simply computed the functions I needed without mapping the variables onto them. Here's the code:
t = 2.8
a = 1
kx = range(-pi/a,stop = pi/a, length=100)
ky = range(-pi/a,stop = pi/a, length=100)
#e_pos = +t*np.sqrt(3 + 4*np.cos(3*KX*a/2)*np.cos(np.sqrt(3)*KY*a/2) + 2*np.cos(np.sqrt(3)*KY*a))
e_pos(kx,ky) = t*sqrt(3+4cos(3*kx*a/2)*cos(sqrt(3)*ky*a/2) + 2*cos(sqrt(3)*ky*a))
e_neg(kx,ky) = -t*sqrt(3+4cos(3*kx*a/2)*cos(sqrt(3)*ky*a/2) + 2*cos(sqrt(3)*ky*a))
# Sort of broadcasting?
e_posfunc = e_pos.(kx,ky);
e_negfunc = e_neg.(kx,ky);
For the plotting I simply used the GR backend:
using Plots
gr()
plot(kx,ky,e_pos,st=:surface)
plot!(kx,ky,e_neg,st=:surface, xlabel="kx", ylabel="ky",zlabel="E(k)")
I got what I wanted!
I am trying to create a cluster point on a map using. This is the code i have used but i am getting the error "list indices must be integers or slices, not float"
# Create map
map_clusters = folium.Map(location=[kol_lat, kol_lng], zoom_start=11)
# Set color scheme for the clusters
x = np.arange(kclusters)
ys = [i + x + (i*x)**2 for i in range(kclusters)]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]
# Add markers to the map
markers_colors = []
for lat, lon, poi, cluster in zip(kolkata_merged['Latitude'], kolkata_merged['Longitude'], kolkata_merged['Neighbourhood'], kolkata_merged['Cluster Labels']):
label = folium.Popup(str(poi) + ' (Cluster ' + str(cluster + 1) + ')', parse_html=True)
map_clusters.add_child(
folium.features.CircleMarker(
[lat, lon],
radius=5,
popup=label,
color=rainbow[cluster-1],
fill=True,
fill_color=rainbow[cluster-1],
fill_opacity=0.7))
map_clusters
Make sure there are no NaN values in your dataframe.
Then try using color=rainbow[int(cluster)-1],
I'm using the basemap library to display spatial information from Copernicus program.
The issue is i can not figure out how to project the data on the robin projection, but I do it correctly with the orthogonal projection.
So currently, I tried this :
plt.ioff()
# adapt for location of datasources
filePath = '../data/grib/download.grib'
# load data
grbs = grb.open(filePath)
grbs.seek(0)
data, lats, lons = (None, None, None)
dataUnit = None
title = None
for g in grbs:
data, lats, lons = g.data()
name = g.name
level = g.level
pressureUnit = g.pressureUnits
date = g.validDate
dataUnit = g.units
title = name + ' at ' + str(level) + ' ' + str(pressureUnit) + ' [' + str(date) + ']'
print(title)
break
# mapPlot = Basemap(projection='ortho', lat_0=0, lon_0=0)
mapPlot = Basemap(projection='robin', lat_0=0, lon_0=0, resolution='l')
mapPlot.drawcoastlines(linewidth=0.25)
x, y = mapPlot(lons, lats)
mapPlot.contourf(x, y, data)
mapPlot.colorbar(location='bottom', format='%.1f', label=dataUnit)
plt.title(title)
plt.show()
The orthogonal projection works correctly. But for the robin projection, I have an ... interesting pattern.
What I'm doing wrong ?
So i figure out how to do. I was misled but the first examples I saw.
Here is a my code:
import matplotlib
from mpl_toolkits.basemap import Basemap, shiftgrid
import matplotlib.pyplot as plt
import numpy as np
import pygrib as grb
# Get data
data = g['values']
lats = g['distinctLatitudes'] # 1D vector
lons = g['distinctLongitudes'] # 1D vector
# Useful information for late
name = g.name
level = str(g.level) + g.pressureUnits
date = g.validDate
dataUnit = g.units
# Parse the data
# Shit the data to start à -180. This is important to mark the data to start at -180°
data, lons = shiftgrid(180., data, lons, start=False) # shiftgrid
# Choose a representation (works with both)
# mapPlot = Basemap(projection='ortho', lat_0=0, lon_0=0)
mapPlot = Basemap(projection='robin', lat_0=0, lon_0=0)
mapPlot.drawcoastlines(linewidth=0.25)
# Convert the coordinates into the map projection
x, y = mapPlot(*np.meshgrid(lons, lats))
# Display data
map = mapPlot.contourf(x, y, data, levels=boundaries, cmap=plt.get_cmap('coolwarm'))
# Add what ever you want to your map.
mapPlot.nightshade(date, alpha=0.1)
# Legend
mapPlot.colorbar(map, label=dataUnit)
# Title
plt.title(name + ' at ' + str(level) + ' [' + str(date) + ']')
plt.show()
So it returns what I'm expecting.
I would like to configure my default options in HoloViews to match those I have been using in my Bokeh plots, but while I can find many equivalents in the HoloViews documentation, I can't figure out what the equivalents are for others.
For example, I have started with the ones I can find in the HoloViews documentation using
opts.defaults(
opts.Scatter(fill_color='black', line_color='gray', fill_alpha=0.1, line_alpha=1.0,
hover_fill_color='yellow', hover_line_color='black', hover_fill_alpha=1.0, hover_line_alpha=1.0,
nonselection_fill_color='gray', nonselection_line_color=None, nonselection_alpha=0.2,
selection_fill_color='black', selection_line_color='white', selection_alpha=1.0,
size=6, line_width=1),
opts.Histogram(fill_color='gray', fill_alpha=0.9, line_width=1, line_color='gray'),
opts.Text(text_color='green')
)
but for many others, especially relating to fonts and control over tick length and colors, I can't find equivalents. In Bokeh I can set these options that I'm interested in for for a given plot with something like
p = figure(...)
# ...
p.xaxis.axis_label = x_label
p.yaxis.axis_label = y_label
p.xaxis.axis_label_text_font = FONT
p.axis.axis_label_text_color = "gray"
p.axis.axis_label_text_font_style = "normal"
p.axis.axis_line_color = "gray"
p.axis.major_label_text_color = "gray"
p.axis.major_tick_line_color = "gray"
p.axis.minor_tick_line_color = "gray"
p.axis.minor_tick_in = 0
p.axis.major_tick_in = 0
p.axis.major_tick_out = 5
p.axis.minor_tick_out = 2
p.grid.grid_line_alpha = 0.5
p.grid.grid_line_dash = [6, 4]
p.title.text_color = "gray"
p.title.text_font = FONT
p.title.text_font_style = "normal"
p.title.align = "center"
p.toolbar.autohide = True
but I'm unsure how to set these in HoloViews using opts.defaults.
How do I set these options using HoloViews? Is there perhaps some general mechanism to "pass" these Bokeh options to HoloViews in opts.defaults?
According to documentation you should be able to get a reference to the Bokeh Figure object and set at least some attributes using plot hooks:
import numpy as np
import holoviews as hv
hv.extension('bokeh')
def hook(plot, element):
print('plot.state: ', plot.state)
print('plot.handles: ', sorted(plot.handles.keys()))
print(plot.handles['xaxis'])
print(plot.state.grid)
print(plot.state.title)
plot.state.title.align = "center"
plot.state.title.text = 'Scatter Plot'
plot.handles['xaxis'].minor_tick_in = 0
plot.handles['xaxis'].major_tick_in = 0
plot.handles['xaxis'].major_tick_out = 5
plot.handles['xaxis'].minor_tick_out = 2
plot.handles['xaxis'].axis_label = 'X-AXIS-GREEN'
plot.handles['yaxis'].axis_label = 'Y-AXIS-RED'
plot.handles['xaxis'].axis_label_text_color = 'green'
plot.handles['yaxis'].axis_label_text_color = 'red'
plot.handles['yaxis'].axis_label_text_color = 'red'
scatter = hv.Points(np.random.randn(1000, 2))
scatter = scatter.opts(hooks = [hook])
renderer = hv.renderer('bokeh')
renderer.save(scatter, 'testHV')
Result:
I am working on image processing and I need to segment colour in image into several groups.I am using k-means algorithm to do that.This time, I want to give English name to the colour which already been clustering. this is my coding:
cform = makecform('srgb2lab');
lab = applycform(a,cform);
ab = double(lab(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors=6;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', 'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
as far as I understand, cluster_center is refer to a*b (lab colour space) value. Is it correct? If so, why I get the value of cluster_center more than range value already set (-128 to 128 for lab color space).