Pygraphviz and fixed node positions - pygraphviz

How do I force pygraphviz to maintain fixed positions for my nodes. Assume that you have the following code
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
import pygraphviz as pgv
from _operator import pos
A=pgv.AGraph()
A.add_node(1,color='red',pos="0,1")
A.add_node(2,color='blue',pos="1,10")
A.add_node(3,color='yellow'pos="2,2")
A.add_edge(1,2,color='green')
A.add_edge(2,3)
A.add_edge(2,2,"1")
A.add_edge(1,3)
A.graph_attr['epsilon']='0.001'
print(A.string()) # print dot file to standard output
A.layout('dot') # layout with dot
A.draw('foo.pdf') # write to file
How do I force the nodes to show up at predetermined positions (0.1), (1,10 and respective (2,2)

Looking at http://pygraphviz.github.io/documentation/pygraphviz-1.4rc1/reference/agraph.html and the signature for the draw method, it looks as if layout is only needed if pos is not present.
"
If prog is not specified and the graph has positions (see layout()) then no additional graph positioning will be performed."
In your case you could just try without using A.layout(). just A.draw('foo.pdf')

Related

Why import numpy doesn't automatically include matlib

I'm trying to repeat a numpy array x horizontally using a=numpy.matlib.repmat(x,1,3). However, directly typing this results in error. I must add import numpy.matlib in order for a=numpy.matlib.repmat(x,1,3) to work.
My question is, why do I need to explicitly import numpy.matlib? I thought import numpy should automatically import everything that appears in the form numpy.* just like numpy.zeros, numpy.array and numpy.mean.

How to connect to AWS Neptune (graph database) with Python?

I'm following this tutorial:
https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-python.html
How can I add a node and then retrieve the same node?
from __future__ import print_function # Python 2/3 compatibility
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
graph = Graph()
remoteConn = DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin','g')
g = graph.traversal().withRemote(remoteConn)
print(g.V().limit(2).toList())
remoteConn.close()
All the above right now is doing is retrieving 2 nodes right?
If you want to add a vertex and then return the information about the vertex (assuming you did not provide your own ID) you can do something like
newId = g.addV("mylabel").id().next()

Checking if a geocoordinate point is land or ocean with cartopy?

I want to know given a latitude and longitude if a coordinate is land or sea
According to https://gis.stackexchange.com/questions/235133/checking-if-a-geocoordinate-point-is-land-or-ocean
from mpl_toolkits.basemap import Basemap
bm = Basemap() # default: projection='cyl'
print bm.is_land(99.675, 13.104) #True
print bm.is_land(100.539, 13.104) #False
The problem is that basemap is deprecated. how di perform this with cartopy?
A question which deals with point containment testing of country geometries using cartopy can be found at Polygon containment test in matplotlib artist.
Cartopy has the tools to achieve this, but there is no built-in method such as "is_land". Instead, you need to get hold of the appropriate geometry data, and query that using standard shapely predicates.
import cartopy.io.shapereader as shpreader
import shapely.geometry as sgeom
from shapely.ops import unary_union
from shapely.prepared import prep
land_shp_fname = shpreader.natural_earth(resolution='50m',
category='physical', name='land')
land_geom = unary_union(list(shpreader.Reader(land_shp_fname).geometries()))
land = prep(land_geom)
def is_land(x, y):
return land.contains(sgeom.Point(x, y))
This gives the expected results for two sample points:
>>> print(is_land(0, 0))
False
>>> print(is_land(0, 10))
True
If you have access to it, fiona will make this easier (and snappier):
import fiona
import cartopy.io.shapereader as shpreader
import shapely.geometry as sgeom
from shapely.prepared import prep
geoms = fiona.open(
shpreader.natural_earth(resolution='50m',
category='physical', name='land'))
land_geom = sgeom.MultiPolygon([sgeom.shape(geom['geometry'])
for geom in geoms])
land = prep(land_geom)
Finally, I produced (back in 2011) the shapely.vectorized functionality to speed up this kind of operation when testing many points at the same time. The code is available as a gist at https://gist.github.com/pelson/9785576, and produces the following proof-of-concept for testing land containment for the UK:
Another tool you may be interested in reading about is geopandas, as this kind of containment testing is one of its core capabilities.

Use ipywidgets to interatively find best position matplotlib text

I am interested in using the interact function to use a slider to adjust the position of text in a matplotlib plot (you know, instead of adjusting the position, running the code, and repeating 1000 times).
Here's a simple example of a plot
import matplotlib.pyplot as plt
x=0.2
y=0.9
plt.text(x, y,'To move',size=19)
plt.show()
and some interact code
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
def f(x):
return x
interact(f, cx=0.2)
I'm wondering how I can combine these to generate a plot with the text along with a slider that will interactively move the text based on the specified value for x. Is this possible? What if I want to do the same for y?
Thanks in advance!
Here you go:
%matplotlib inline
import matplotlib.pyplot as plt
from ipywidgets import interact
def do_plot(x=0.2, y=0.9):
plt.text(x, y,'To move',size=19)
plt.show()
interact(do_plot)

Pandas does not plot if label includes Turkish Characters like ş,ö,ü

My problem, I can't plot when label include turkish characters like ş,ö,ü,İ,Ğ,ğ
it just gives this output --> matplotlib.figure.Figure at 0x7f8499386050 not show the graph
How can I fix it?
here is my code:
def draw_pie(year,y,by):
v=data[data['yil']==year]
v=v.sort(y,ascending=False).head(20)
v=v.groupby(by).aggregate('sum')
veri= v[ y ]
veri.plot(figsize=(10,10),kind='pie', labels=v.index,
autopct='%.2f', fontsize=20)
plt.show()
draw_pie(2014,'toplam_hasilat','tur')
don't show beacuse tur contains 'Aşk' , 'Gençlik'
it is okay without turkish characters.
Thank you in advance
Assuming you decide to use universal encoding:
You first have to tell the source encoding is utf-8 (first line below). Then you have to decode the string as utf-8. If python3 it should work, if python2 you can tell it to behave like python3:
Example (Python 2.7.8)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.title("Simple Plot şöüİĞğ")
plt.show()
You can also explicitely decode each string but that might not be very convenient (two solutions below)
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
plt.plot(range(10), range(10))
plt.title("Simple Plot şöüİĞğ".decode('utf-8'))
plt.xlabel(u"Simple Plot şöüİĞğ")
plt.show()
I've used the solution for my problem as follows.
I was trying to read data from Excel with Turkish fonts. Pandas failed in plotting the data in my Jupyter noteboook. Then I added
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
in the first cell, ran the all cells. Then I got my plots.