How to plot several coordinates from pymongo plotly and mapbox - pandas

I'm working in a project in which i need to plot several coordinates from a mongodb query, i can plot a single result and works awesome, but i need to figure out how to plot several coordinates from the query, the query contains the info needed but i don;t know how to iterate correctly in the result file i think that's my problem , i'm running on circles now so i need some help .
Code that works for a single result :
countE2 = mydb.results.find({"d.latitude":{"$gt":0},"d.longitude":{"$gt":0}},{"d.latitude":1, "d.longitude": 1,"a":1,"u.Server":1}).limit(10).sort('t', pymongo.DESCENDING)
for resultado in countE2:
lat = resultado["d"]["latitude"]
lon = resultado["d"]["longitude"]
ip = resultado["a"]
server = resultado["u"]["Server"]
us_cities = [lat,lon,ip,server]
y = us_cities
print(y)
fig = px.scatter_mapbox(y,lat=[y[0]], lon=[y[1]],hover_name=[y[2]],hover_data=[[y[3]],[y[3]]],color_discrete_sequence=["green"], zoom=3, height=1000)
print(fig)
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
Please consider that the query returns several results ,in this case countE.
Thanks

Related

ggplot geom_bar with errors not printing

Assigning means and se to a data frame and trying to construct geom_bar with error bars for 2 experimental treatments across two genders
library(ggplot2)
dff <- data.frame(group=c('NSI','NSI','SI','SI'),
gender = c('Female','Male','Female','Male'),
mean.Score =c(3.41,3.3,2.63,3.32),
se =c(1.92,2.03,1.73,2.21))
dff$group <- as.factor(dff$group)
dff$gender <- as.factor(dff$gender)
p <- ggplot(dff,aes(x= group,y=mean.Score,fill=gender))+
scale_fill_manual(values = c("#F34444", "#0066CC"))+
geom_bar(position = 'dodge',stat = 'identity',width=1.8)+
geom_errorbar(aes(ymin=mean.Score-se, ymax=mean.Score+se),
width=.2,
position=position_dodge(1.8))+
theme(plot.title = element_text(size = 10,hjust = 0.9))+
scale_x_discrete(limits=c('NSI','NSI','SI','SI'))+
ggtitle("Performance by Treatment & Gender")
plot(p)
Two treatments: NSI and SI. Across two genders: Female and Male; data is corresponding mean performance and standard error of that performance. Assigning to a data frame and trying to plot a histogram with error bars of the data. Code executes fine in the window, but then nothing shows up in the Plot window. Thanks for any help for a relative newbie!
Mary
probably go to tools > global options > pane layout, and make sure the plot is checked for the console window
example of where to check in rstudio

Filtering on relation

I'm trying to retrieve a network of the major roads in Norway. However, the "int_ref" and "ref" labels are inconsistent and are resulting in gaps in the road. When looking at the road in OpenStreetMap I can see that the 'relation' tag under 'Part of' is exactly what I need. Is there any way to retrieve this using OSMnx? Is there any other way to retrieve a full road? I'm using the following line of code when filtering one specific road based on int_ref:
G1 = ox.graph_from_place(query = "Norway", retain_all = True, custom_filter = '["int_ref"~"E 39"]')
No, OSMnx filters on way tags, not on relations. If you want to get only the major roads in a country, see this answer: https://stackoverflow.com/a/52412274/7321942
Something like this may do what you are looking for:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
# get the geometry of the norwegian mainland
gdf = ox.geocode_to_gdf('Norway')
geom = max(gdf['geometry'].iloc[0], key=lambda x: x.area)
# get all motorway/trunk roads
cf = '["highway"~"motorway|motorway_link|trunk|trunk_link"]'
G = ox.graph_from_polygon(geom, network_type='drive', custom_filter=cf)
# plot it
fig, ax = ox.plot_graph(G)
It takes ~370 Overpass API requests to download all the area of the Norwegian mainland, so it takes a while to make all those requests. You can watch its progress in the log in the console.

Zooming a pherical projection in matplotlib

I need to display a catalogue of galaxies projected on the sky. Not all the sky is relevant here, so I need to center an zoom on the relevant part. I am OK with more or less any projection, like Lambert, Mollweide, etc. Here are mock data and code sample, using Mollweide:
# Generating mock data
np.random.seed(1234)
(RA,Dec)=(np.random.rand(100)*60 for _ in range(2))
# Creating projection
projection='mollweide'
fig = plt.figure(figsize=(20, 10));
ax = fig.add_subplot(111, projection=projection);
ax.scatter(np.radians(RA),np.radians(Dec));
# Creating axes
xtick_labels = ["$150^{\circ}$", "$120^{\circ}$", "$90^{\circ}$", "$60^{\circ}$", "$30^{\circ}$", "$0^{\circ}$",
"$330^{\circ}$", "$300^{\circ}$", "$270^{\circ}$", "$240^{\circ}$", "$210^{\circ}$"]
labels = ax.set_xticklabels(xtick_labels, fontsize=15);
ytick_labels = ["$-75^{\circ}$", "$-60^{\circ}$", "$-45^{\circ}$", "$-30^{\circ}$", "$-15^{\circ}$",
"$0^{\circ}$","$15^{\circ}$", "$30^{\circ}$", "$45^{\circ}$", "$60^{\circ}$",
"$75^{\circ}$", "$90^{\circ}$"]
ax.set_yticklabels(ytick_labels,fontsize=15);
ax.set_xlabel("RA");
ax.xaxis.label.set_fontsize(20);
ax.set_ylabel("Dec");
ax.yaxis.label.set_fontsize(20);
ax.grid(True);
The result is the following:
I have tried various set_whateverlim, set_extent, clip_box and so on, as well as importing cartopy and passing ccrs.LambertConformal(central_longitude=...,central_latitude=...) as arguments. I was unable to get a result.
Furthermore, I would like to shift RA tick labels down, as they are difficult to read with real data. Unfortunately, ax.tick_params(pad=-5) doesn't do anything.

Convert date/time index of external dataset so that pandas would plot clearly

When you already have time series data set but use internal dtype to index with date/time, you seem to be able to plot the index cleanly as here.
But when I already have data files with columns of date&time in its own format, such as [2009-01-01T00:00], is there a way to have this converted into the object that the plot can read? Currently my plot looks like the following.
Code:
dir = sorted(glob.glob("bsrn_txt_0100/*.txt"))
gen_raw = (pd.read_csv(file, sep='\t', encoding = "utf-8") for file in dir)
gen = pd.concat(gen_raw, ignore_index=True)
gen.drop(gen.columns[[1,2]], axis=1, inplace=True)
#gen['Date/Time'] = gen['Date/Time'][11:] -> cause error, didnt work
filter = gen[gen['Date/Time'].str.endswith('00') | gen['Date/Time'].str.endswith('30')]
filter['rad_tot'] = filter['Direct radiation [W/m**2]'] + filter['Diffuse radiation [W/m**2]']
lis = np.arange(35040) #used the number of rows, checked by printing. THis is for 2009-2010.
plt.xticks(lis, filter['Date/Time'])
plt.plot(lis, filter['rad_tot'], '.')
plt.title('test of generation 2009')
plt.xlabel('Date/Time')
plt.ylabel('radiation total [W/m**2]')
plt.show()
My other approach in mind was to use plotly. Yet again, its main purpose seems to feed in data on the internet. It would be best if I am familiar with all the modules and try for myself, but I am learning as I go to use pandas and matplotlib.
So I would like to ask whether there are anyone who experienced similar issues as I.
I think you need set labels to not visible by loop:
ax = df.plot(...)
spacing = 10
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
if label not in visible:
label.set_visible(False)

Trouble converting UTM to lat long for southern hemisphere

I have ~40 points in UTM zone 19 taken from Peru that I would like to convert to lat/long to project onto Google Earth. I am having some problems with PBSmapping and can't seem to figure out the solution. I have searched through the forums and tried several different methods, including the project command in proj4 but still can't get this to work. Here is the code I have currently written
library(PBSmapping)
#just two example UTM coordinates
data<-as.data.frame(matrix(c(214012,197036,8545520,8567292),nrow=2))
attr(data,"projection") <- "UTM"
attr(data, "zone") <- 19
colnames(data)<-c("X","Y")
convUL(data,km=FALSE)
The corresponding lat/longs should be somewhere with lats between -12.9XXXXX and -13.0XXXXX and long between -71.8XXXX to -71.4XXXX. The values given by convUL seem to be way off.
Once you get the valid pairs of coordinates you could do something like this:
library(rgdal)
data <- data.frame(id = c(1:2), x = c(214012,197036) , y = c(8545520,8567292))
coordinates(data) = ~x+y
Asign projection
# Use the appropriate EPSG Code
proj4string(data) = CRS('+init=epsg:24891') # 24891 or 24893
Transform to geographic coordinates
enter code heredata_wgs84 <- spTransform(data, CRS('+init=epsg:4326'))
Get some valid background data to plot it against
# Country data
package(dismo)
peru <-getData('GADM', country='PER', level=0)
plot(peru, axes = T)
plot(data, add = T)
Write your KML file
# Export kml
tmpd <- 'D:\\'
writeOGR(data_wgs84, paste(tmpd, "peru_data.kml", sep="/"), 'id', driver="KML")