I'm trying to model voting dynamics on networks, and would like to be able to create a graph in NetworkX where I can iterate the voter process on nodes, having their colour change corresponding to their vote 'labels'.
I've managed to get this code to let me see the attributes for each node, but how do I go about using those in a for loop to designate colour?
H = nx.Graph()
H.add_node(1,vote='labour')
H.add_node(2,vote='labour')
H.add_node(3,vote='conservative')
h=nx.get_node_attributes(H,'vote')
h.items()
Gives me the result:
[(1, 'labour'), (2, 'labour'), (3, 'conservative')]
I've got a for loop to do this type of colour coding based on the node number as follows, but haven't managed to make it work for my 'vote' status.
S=nx.star_graph(10)
colour_map=[]
for node in S:
if node % 2 ==0:
colour_map.append('blue')
else: colour_map.append('yellow')
nx.draw(S, node_color = colour_map,with_labels = True)
plt.show()
You can iterate the node attributes with H.nodes(data=True) which returns the node name and the node attributes in a dictionary. Here's a full example using your graph.
import networkx as nx
import matplotlib.pyplot as plt
H = nx.Graph()
H.add_node(1, vote='labour')
H.add_node(2, vote='labour')
H.add_node(3, vote='conservative')
color_map = []
for node, data in H.nodes(data=True):
if data['vote'] == 'labour':
color_map.append(0.25) # blue color
elif data['vote'] == 'conservative':
color_map.append(0.7) # yellow color
nx.draw(H, vmin=0, vmax=1, cmap=plt.cm.jet, node_color=color_map, with_labels=True)
plt.show()
This code will draw a different layout of nodes each time you run it (some layouts, as e.g. draw_spring, are available here).
Regarding colors, I use 0.25 for blue and 0.7 for yellow. Note that I use the jet matplotlib colormap and that I set vmin=0 and vmax=1 so that the color values are absolute (and not relative to eachother).
Output of the code above:
UPDATE:
I wasn't aware that you could simply use color names in matplotlib. Here's the updated for loop:
for node, data in H.nodes(data=True):
if data['vote'] == 'labour':
color_map.append("blue")
elif data['vote'] == 'conservative':
color_map.append("yellow")
And the updated draw command:
nx.draw(H, node_color=color_map, with_labels=True)
Note that this way you get different shades of blue and yellow than in the image above.
Related
I have an elevation raster layer in my GeoServer with a single channel ("gray").
The "gray" values is elevations values (signed int16).
I have 2 clients:
The first one is using that elevation values as is.
The second one expect to get [Mapbox Terrain-RGB format][1]
I do not want to convert the "gray scale" format to Mapbox Terrain-RGB format and hold duplicate data in the GeoServer.
I was thinking to use the SLD style and elements to map the elevation value to the appropriate RGB value (with gradient interpolation between discrete values).
For example:
<ColorMap>
<ColorMapEntry color="#000000" quantity="-10000" />
<ColorMapEntry color="#FFFFFF" quantity="1667721.5" />
</ColorMap>
It turns out that the above example does not span the full range of colors but rather creates gray values only.
That is, it seems that it interpolate each color (red, green, blue) independent of each other.
Any idea how to make it interpolate values like that: #000000, #000001, #000002, ... , #0000FF, #000100, ..., #0001FF, ..., #FFFFFF?
Tx.
[1]: https://docs.mapbox.com/data/tilesets/reference/mapbox-terrain-rgb-v1/
I'm trying to do the same with no luck, and i think it can't be done... Check this example. It's a "gradient" [-10000, -5000, -1000, -500 ... 100000000000000000, 5000000000000000000, 1000000000000000000] with the Mapbox color codification. The color progression/interpolation is anything but linear, so i think it can't be emulated in an SLD.
If you have the elevation data in the format you desire then that is the easiest option: it just works. However, if you want a more customized solution, here's what I've done for a project using the Mapbox Terrain-RGB format:
I have a scale of colors from dark blue to light blue to white.
I want to be able to specify how many steps are used from light blue to white (default is 10).
This code uses GDAL Python bindings. The following code snippet was used for testing.
It just outputs the color mapping to a GeoTIFF file.
To get values between 0 and 1, simply use value *= 1/num_steps.
You can use that value in the lookup table to get an RGB value.
If you're only interested in outputting the colors, you can ignore everything involving gdal_translate. The colors will automatically be stored in a single-band GeoTIFF. If you do want to re-use those colors, note that this version ignores alpha values (if present). You can use gdal_translate to add those. That code snippet is also available at my gist here.
import numpy as np
import gdal
from osgeo import gdal, osr
def get_color_map(num_steps):
colors = np.zeros((num_steps, 3), dtype=np.uint8)
colors[:, 0] = np.linspace(0, 255, num_steps, dtype=np.uint8)
colors[:, 1] = colors[::-1, 0]
return colors
ds = gdal.Open('/Users/myusername/Desktop/raster.tif')
band = ds.GetRasterBand(1) # Assuming single band raster
arr = band.ReadAsArray()
arr = arr.astype(np.float32)
arr *= 1/num_steps # Ensure values are between 0 and 1 (or use arr -= arr.min() / (arr.max() - arr.min()) to normalize to 0 to 1)
colors = get_color_map(num_steps) # Create color lookup table
colors[0] = [0, 0, 0] # Set black for no data so it doesn't show up as gray in the final product.
# Create new GeoTIFF with colors included (transparent alpha channel if possible). If you don't care about including the colors in the GeoTIFF, skip this.
cols = ds.RasterXSize
rows = ds.RasterYSize
out_ds = gdal.GetDriverByName('GTiff').Create('/Users/myusername/Desktop/raster_color.tif', cols, rows, 4)
out_ds.SetGeoTransform(ds.GetGeoTransform())
out_ds.SetProjection(ds.GetProjection())
band = out_ds.GetRasterBand(1)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(2)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(3)
band.WriteArray(colors[arr]) # This can be removed if you don't care about including the colors in the GeoTIFF
band = out_ds.GetRasterBand(4)
alpha = np.zeros((rows, cols), dtype=np.uint8) # Create alpha channel to simulate transparency of no data pixels (assuming 0 is "no data" and non-zero is data). You can remove this if your elevation values are not 0.
alpha[arr == 0] = 255
band.WriteArray(alpha) # This can be removed if you don't care about including the colors in the GeoTIFF
out_ds.FlushCache()
This issue is also present in Rasterio when using a palette with multiple values. Here is an example.
However, if your raster has n-dimensions or is a masked array, the flip operation can be tricky. Here's a solution based on one of the answers in this stackoverflow question: How to vertically flip a 2D NumPy array?.
I am new to netwrokx and I have a big network as follows that I need to just visualize its blue nodes:
Is there any way to see just blue nodes while the distance between them is same as the real graph's?
My desired output would be something like following one:
Result of using pos layout is as follows:
Joel helped med to find out the result and I share codes and outcome here for those who have the similar question:
Answer Codes:
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, nodelist = blue_nodes, node_color =
'blue',with_labels=False)
outcome:
Given a network G, with a list of "blue" nodes bluenodes, we can define a set of positions, and then draw just the blue nodes.
pos = nx.spring_layout(G) #there are other layouts that you might want to try.
nx.draw_networkx_nodes(G, pos, nodelist = bluenodes, node_color = 'blue', with_labels=False)
I am using basemap on Python 2.7 but would like to go for Python 3, and therefor, moving to cartopy. It would be fantastic if you would give me some advises how to change my code from basemap to cartopy:
This is the basemap code:
from mpl_toolkits.basemap import Basemap
# plot map without continents and coastlines
m = Basemap(projection='kav7',lon_0=0)
# draw map boundary, transparent
m.drawmapboundary()
m.drawcoastlines()
# draw paralells and medians, no labels
if (TheLatInfo[1] == len(TheLatList)) & (TheLonInfo[1] == len(TheLonList)):
m.drawparallels(np.arange(-90,90.,30.))
m.drawmeridians(np.arange(-180,180.,60.))
grids = m.pcolor(LngArrLons,LngArrLats,MSKTheCandData,cmap=cmap,norm=norm,latlon='TRUE')
This is the cartopy example I found and have changed some bits:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cpf
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines()
ax.set_boundary
ax.gridlines(draw_labels=False)
plt.show()
I am not sure about how to set the gridlines in the exact positions and how to color them black instead of grey. Furthermore, I wonder how to insert/overlay my actual map with data then. Is "ax.pcolor" well enough supported by cartopy?
Thank you!
To color your gridlines black, you can use a color= keyword:
ax.gridlines(color='black')
To specify lat/lon gridline placement, you really only need a few extra lines, if you don't care about labels:
import matplotlib.ticker as mticker
gl = ax.gridlines(color='black')
gl.xlocator = mticker.FixedLocator([-180, -90, 0, 90, 180])
gl.ylocator = mticker.FixedLocator([-90,-45,0,45,90])
(As of writing this, Robinson projections don't support gridline labels.)
To overlay your data on the map,pcolor should work, but it's famously slow. I would recommend pcolormesh, though you can substitute one for another in this syntax:
ax.pcolormesh(lon_values, lat_values, data)
Note that if your data come on a different projection than the map projection you're plotting (typically true), you need to specify the data's projection in the plotting syntax using the transform= keyword. That tells cartopy to transform your data from their original projection to that of the map. Plate Carrée is the same as cylindrical equidistant (typical for climate model output, for example):
ax.pcolormesh(lon_values, lat_values, data, transform=ccrs.PlateCarree())
This question already exists:
How to accurately classify leafs into its disease category using Matlab
Closed 6 years ago.
I have an image of leaf that has mostly three colors black background, green leaf and brown diseased spots.
Here is the image
When I cluster it first time, I get brown spots in cluster 1, green portion in cluster 2, black region in cluster 3(for example).
When I cluster it second time, I get green portion in cluster 1,brown spots in cluster 2, black region in cluster 3(for example). b
When I cluster it third time the order of clusters are different again.
I want to change the code such that the brown spots appear in cluster 1, green portion in cluster 2 and black in cluster 3. The order of clusters should be same even if I cluster many times.Could someone please help me with the code? I am using Matlab2009a. This question is about ordering clusters
Here is what is being done so far
function segmented_img = leaf_segmentation( original_img, nclusters )
original_img = im2double(original_img);
G=fspecial('gaussian',[200 250],1);
smoothed_img =imfilter(original_img,G,'same');
conversionform = makecform('srgb2lab');
lab_img = applycform(smoothed_img,conversionform);
ab_img = double(lab_img(:,:,2:3));
nrows = size(ab_img,1);
ncols = size(ab_img,2);
ab_img = reshape(ab_img,nrows*ncols,2);
cluster_idx =
kmeans(ab_img,nclusters,'distance','sqEuclidean','Replicates',3);
cluster_img = reshape(cluster_idx,nrows,ncols);
segmented_img = cell(1,nclusters);
for k = 1:nclusters
segmented_img{k} = bsxfun( #times, original_img, cluster_img == k );
end
end
segmented = leaf_segmentation( imread('input image'), 3 );
figure,imshow(segmented{1}), title('Cluster 1');
figure, imshow(segmented{2}), title('Cluster 2');
figure, imshow(segmented{3}), title('Cluster 3');
Matlab kmeans has a 'Start' parameter, which can be set to a matrix of centroid initial locations. You can initialize to black, brown and green and you will probably even get the results a bit faster, if the image is really mostly composed of these colours.
Documentation
Just check the colors after the clustering!
One way of doing this is transforming the centroids color space to HSV and checking the values oh H and V. H will give you the color (for example, around 120.0 degrees its green) and V will give you the "ligth", so if V is 0 then whatever H is, it is the black cluster.
This should be trivial to program, but dont hesitate to ask any questions about it.
I have created a graph with networkx and have wrote the graph representation to a dot file to be displayed with graphviz. Now, the nodes have color attributes and I would like graphviz to place nodes with the same color closer to each other.
For example, if node "soccer" and node "football" both have color 'blue' then they should be close together, whereas node "baseball" with color 'green' would not be near nodes "soccer" and "football"
How can I get nodes with the same color to be drawn closer together in Graphviz; hence forming clusters of colors?
Thanks for all the help and let me know if you need more information :)
You could use PyGraphviz to do the layout using dot with "clusters".
e.g.
import networkx as nx
G = nx.Graph()
G.add_node(1, color='blue', style='filled')
G.add_node(2, color='red', style='filled')
G.add_edge(1,2)
G.add_node(3, color='blue',style='filled')
G.add_node(4, color='red',style='filled')
G.add_edge(3,4)
G.add_edge(4,10)
G.add_path([10,20,30,40,50])
A = nx.to_agraph(G) # uses pygraphviz
red_nodes = [n for n,d in G.node.items() if d.get('color')=='red']
blue_nodes = [n for n,d in G.node.items() if d.get('color')=='blue']
A.add_subgraph(red_nodes, name = 'cluster1', color='red')
A.add_subgraph(blue_nodes, name = 'cluster2', color='blue')
A.write('colors.dot')
A.layout('dot')
A.draw('colors.png')