OSMNX - trying to plot graph in console without any luck - python

I have the following code
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, LineString
import matplotlib.pyplot as plt
import networkx as nx
import osmnx as ox
ox.config(use_cache=True, log_console=True)
G = ox.graph_from_point((37.79, -122.41), dist=750, network_type='all')
ox.plot_graph(G)
plt.show()
I am simply trying to plot a simple figure in the console, but there is no output at all. The above code is written in a script. Anyone that knows what the issue is?
Platform: Spyder (Python 3.8)

I fixed the issue. To make the plots appear in the Console, I had to uncheck "Mute Inline Plotting" under Plots pane options menu.

Related

my pandas and seaborn comands not responding

import pandas as pd
dataFrame = pd.read_excel("C:/Users/****/desktop/python folder/tensorflow/sheet.xlsx")
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
Output is: PS C:\Users \ -----\Desktop\python folder> & C:/Users/------/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/-----/Desktop/python folder/tensorflow/tensorflow-101.py"
For your problem, try this.
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
plt.show()
Graph will appear in new window, I guess. It's why your code is working in jupyter, and not in script.
ps. vsc is just a editor, so you can't use vsc for executing python. You are executing python script with your own python program.

How to fix 'NameError: name 'cartopy' is not defined', in plot about sea surface temperature in python?

I'm really new to python, and I need to plot a data netcdf of sea surface temperature(sst),in python, but it keep giving erro.
I use the same code in another notebook, and it run perfect fine.
###SST CĂ“DIGO PLOT
import numpy as np
import matplotlib.pyplot as plt
from numpy import pi
from mpl_toolkits.basemap import Basemap
from netCDF4 import Dataset
import pandas as pd
from scipy import stats
import seaborn as sns
import xarray as xr
import cartopy.crs as ccrs
import os
from netCDF4 import Dataset as netcdf_dataset
from cartopy import config
import statistics
import glob
import seaborn as sns
ds = xr.open_dataset('/home/mayna/Downloads/d86/20190327010000-OSISAF-L3C_GHRSST-SSTsubskin-GOES16-ssteqc_goes16_20190327_010000-v02.0-fv01.0.nc')
plt.figure(figsize=(8,4))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', linewidth=0.25)
ax.coastlines(linewidth=0.25)
ds['sea_surface_temperature'][0,:,:].plot.contourf(levels=20, ax=ax, transform=ccrs.PlateCarree(),cmap='coolwarm')
It say that erro is in the line "ax.add_feature(cartopy.feature.BORDERS, linestyle='-', linewidth=0.25)", that "NameError: name 'cartopy' is not defined".
What you think it is the problem?
P.S.: I know that I'm using a lot of lib that don't need
You seem to have never defined cartopy. Perhaps import cartopy at the top would solve your problem.
Name error in python means that the specific attribute/method is not imported in the program. In the code you are using cartopy.crs, cartopy.config and cartopy.features.Border, but only the first two are imported through your statements
import cartopy.crs as crash
and
from cartopy import config
So for features.Border, you either do
import cartopy
Or
from cartopy import features.Border #use just features.Border in that line if you are doing this.
For me, i installed cartopy package in jupyter but still it was showing error of cartopy not defined.(I was plotting the plots with cartopy.ccrs for stereographic plots)
solution: before importing and executing cartopy.ccrs just execute 'import cartopy' in the first line itself and then run the codes.It worked for me

Pycharm: show x/y coordinates with matplotlib automatically

If I plot with ipython, I automatically see the x/y coordinates when I move with the mouse over the canvas (see bottom right in screenshot):
import matplotlib.pyplot as plt
import numpy as np
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()
How can the same achieved with Pycharm (my plots appear in the SciView toolwindow)?
If not: is there perhaps an easy workaround for it? (and do I have more possibilities if the plot does not appear in the SciView toolwindow?)
using TkAgg it works:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()

Is there any way to ask Basemap not show the plot?

I am trying to use mpl_toolkits.basemap on python and everytime I use a function for plotting like drawcoastlines() or any other, the program automatically shows the plot on the screen.
My problem is that I am trying to use those programs later on an external server and it returns 'SystemExit: Unable to access the X Display, is $DISPLAY set properly?'
Is there any way I can avoid the plot to be shown when I use a Basemap function on it?
I just want to save it to a file so later I can read it externally.
My code is:
from mpl_toolkits.basemap import Basemap
import numpy as np
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))
Use the Agg backend, it doesn't require a graphical environment:
Do this at the very beginning of your script:
import matplotlib as mpl
mpl.use('Agg')
See also the FAQ on Generate images without having a window appear.
The easiest way is to put off the interactive mode of matplotlib.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
#NOT SHOW
plt.ioff()
m = Basemap(projection='robin',lon_0=0)
m.drawcoastlines()
#m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,120.,10.))
m.drawmeridians(np.arange(0.,360.,60.))

Draw graph in NetworkX

I'm trying to draw any graph in NetworkX, but get nothing, not even errors:
import networkx as nx
import matplotlib.pyplot as plt
g1=nx.petersen_graph()
nx.draw(g1)
Add to the end:
plt.show()
import networkx as nx
import matplotlib.pyplot as plt
g1 = nx.petersen_graph()
nx.draw(g1)
plt.show()
When run from an interactive shell where plt.ion() has been called, the plt.show() is not needed. This is probably why it is omitted in a lot of examples.
If you run these commands from a script (where plt.ion() has not been called), the plt.show() is needed. plt.ion() is okay for interactive sessions, but is not recommended for scripts.
in ipython notebook, just type in magic
%matplotlib inline
or
%matplotlib notebook
You can easily plot with networkx graphs using jupyter notebook. See first example.
OR, you can use Bokeh to plot graphs, which adds useful features.
The package holoviews makes it even simpler to plot a graphs with bokeh. It adds features like automatic highlighting and show of labels while hovering over nodes. However, editing colors etc. seems to be an issue.
%pylab inline
# `pylab notebook` # for interactive plots
import pandas as pd
import networkx as nx
import holoviews as hv
G=nx.Graph()
ndxs = [1,2,3,4]
G.add_nodes_from(ndxs)
G.add_weighted_edges_from( [(1,2,0), (1,3,1) , (1,4,-1) , (2,4,1) , (2,3,-1), (3,4,10) ] )
nx.draw(G, nx.spring_layout(G, random_state=100))
And here the example with bokeh and holoview:
hv.extension('bokeh')
%opts Graph [width=400 height=400]
padding = dict(x=(-1.1, 1.1), y=(-1.1, 1.1))
hv.Graph.from_networkx(G, nx.layout.spring_layout).redim.range(**padding)
You should give it a try and plot it in your notebook to see the difference.
It works fine by adding:
import matplotlib.pyplot as plt
plt.show()
to your code. mine worked fine.

Categories

Resources