I've been trying to figure out how to use google chart-wrapper with my django application but I can't seem to find hardly any documentation on how to use it. After playing around with it for awhile I got it to do most everything I want it to, but I can't seem to figure out how to get the range quite right.
For example, this url:
http://chart.apis.google.com/chart?chxt=x&chd=t:2.0,1.0,192.0,1032.0,22.0,60.0,75.0,94.0,3.0,2.0,1.0,11.0,383.0,3.0,164.0,50.0,12.0,5.0,564.0,7671.0,115.0,331.0&chm=N%2A,000000,0,-1,11&chs=1000x300&cht=bvs&chtt=Failure+Types&chxl=0:|B16|B13|B10|C11|A10|A13|D04|D03|D02|B01|C09|B05|B04|B07|C01|C03|C04|C07|A02|A01|A06|A05
has several values above 100 (in the chd section of the url), but when it gets rendered it cuts anything above 100 off.
This is the code I used to create that url:
hist = {}
d = Defect.objects.all()
for defect in d:
c = Failure.objects.filter(defect = defect).count()
if c > 0:
hist[defect.defcode] = c
m = VerticalBarStack(hist.values())
m.title("Failure Types")
m.size(1000,300)
m.marker('N*', 'black', 0, -1, 11)
m.axes('x')
m.axes.label(0, '|'.join(hist.keys()))
It seems logical that google chart-wrapper would allow me to write something along the lines of m.range(1000) or something similar to specify the range to show on the chart, but that doesn't work and I can't find any real documentation or detailed, complex enough examples to figure it out.
You should add scaling for extended encoding.
I have added the required parameters, as below:
chds=0,2000
your final URL is:
http://chart.googleapis.com/chart?chxt=x&chd=t:2.0,1.0,192.0,1032.0,22.0,60.0,75.0,94.0,3.0,2.0,1.0,11.0,383.0,3.0,164.0,50.0,12.0,5.0,564.0,7671.0,115.0,331.0&chm=N%2A,000000,0,-1,11&chs=500x500&cht=bvs&chds=0,2000&chtt=Failure+Types&chxl=0:|B16|B13|B10|C11|A10|A13|D04|D03|D02|B01|C09|B05|B04|B07|C01|C03|C04|C07|A02|A01|A06|A05
Related
I have written a streamlit code where I am using altair to display a chart in the front end. The made that chart to have zoom in/out functionality. I used ".interactive()" to get this done like below.
chart = alt.Chart(embd_1).mark_circle(size=30).encode(
x = 'dimention1:Q',
y = 'dimention2:Q',
tooltip=['col1'] ,
color=color).properties(width=600,height=600).add_selection(selected).interactive()
But, I am facing a problem when I do zoom in/out in my app. It's taking lot of time to get updated. once i start scrolling the mouse, it takes literally more than 10 secs to update the zoomed chart in the front end.
I was just wondering this may be because streamlit is running all the code which is beneath the altairs code as i don't know how to skip or avoid a certain code when i use zoom in/out functionality.
So, The question is how to programatically define whether the user is zooming in/out ?
like below :
if CheckZoom_SomeThingIDnotKnowYet == True:
logic to execute code1
else:
logic to execute code2
Additional Info On data and Altair Code used:
def altair_graph(embd_1):
selected = alt.selection_single(on="click", empty="none")
dom = ['Other IPs', 'Slected IP','Sel Dims']
rng_clr = ['lightgrey', 'red','blue']
color_point=alt.Color('color', scale=alt.Scale(domain=dom, range=rng_clr))
color = alt.condition(selected, alt.value('red'), color_point,legend=None)
chart = alt.Chart(embd_1).mark_circle(size=30).encode(
x = 'dimention1:Q',
y = 'dimention2:Q',
tooltip=['dimention1','dimention2'] ,
color=color
).properties(width=600,height=600).add_selection(selected).interactive()
return chart
And Sample for the above function Can be created like below:
dimention1=np.random.rand(1,100000).squeeze()
dimention2=np.random.rand(1,100000).squeeze()
colr_values = ['Other Ids', 'Slected Id','Sel Dims']
color = np.random.choice(colr_values, 100000, p=[0.9, 0.05, 0.05])
sample = pd.DataFrame({'dimention1':dimention1,'dimention2':dimention2,'color':color})
altair_graph(sample)
As shown in the example, My real time data is more than 150k data points.
Even When I have executed the above code outside streamlit, This is taking significant time to zoom in/out. Could ssomeone please provide a workaround for this problem.
Altair/VegaLite is a the moment not very performant with that many data points (I think it slows down around 20-40k somewhere). You can try alt.data_transformers.enable('data_server') for potential minor improvements and see my answer here for some more details and discussion in the comments https://stackoverflow.com/a/67349827/2166823.
I have the following code below that I got from https://astroquery.readthedocs.io/en/latest/gaia/gaia.html.
I get the br when ra and dec is a number. However, I have not just one number, but a list of ra and dec. When I tried putting a list in for ra and dec in the code below, I got an error saying Error 500: null. Is there a way to find br using a list of ra and dec?
coord = SkyCoord(ra=, dec=, unit=(u.degree, u.degree), frame='icrs')
width = u.Quantity(0.0005, u.deg)
height = u.Quantity(0.0005, u.deg)
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
r.pprint()
r.columns
br=[r["phot_bp_rp_excess_factor"]]
print (br)
I am new to astroquery, so any help will be apreciated.
Hi and congrats on your first StackOverflow question. As I understand it, Astroquery is a community effort, and the modules for querying individual online catalogues are in many cases being developed and maintained side-by-side with the online query systems, often by their same developers. So different modules within Astroquery are being worked on sometimes by different teams, and have some varying degree of feature-completeness and consistency in interfaces (something I'd like to see improved but it's very difficult to coordinate).
In the case of Gaia.query_object_async the docs are not completely clear, but it's not obvious that it even supports array SkyCoord. It should, or at least if it doesn't it should give a better error.
To double check, I dug into the code and found what I kind of suspected: It does not at all allow this. It is building a SQL query using string replacement (generally considered a bad idea) and passing that SQL query to a web-based service. Because the ra and dec values are arrays, it just blindly passes those array representations into the template for the SQL query, resulting in an invalid query:
SELECT
TOP 50
DISTANCE(
POINT('ICRS', ra, dec),
POINT('ICRS', [99.00000712 87.00000767], [24.99999414 24.99999461])
) as dist,
*
FROM
gaiadr2.gaia_source
WHERE
1 = CONTAINS(
POINT('ICRS', ra, dec),
BOX(
'ICRS',
[99.00000712 87.00000767],
[24.99999414 24.99999461],
0.005,
0.005
)
)
ORDER BY
dist ASC
The server, rather than return an error message suggesting that the query is malformed, instead just returns a general server error. Basically it crashes.
Long story short, you should probably open a bug report about this against astroquery, and see if it's on the Gaia maintainers' radar to deal with: https://github.com/astropy/astroquery/issues/new
In the meantime it sounds like your best bet is to make multiple queries in a loop and join their results together. Since it returns a Table, you can use astropy.table.vstack:
from astropy.table import vstack
results = []
for coord in coords:
results.append(Gaia.query_object_async(coord, width=width, height=height))
results = vstack(results)
I am trying to update Span using ColumnDataSource, but the information is not being passed onto the source. Span unfortunately does not have a paremeter "source", so is there a better way?
I have defined my sources, figure and line like so:
m1_source = ColumnDataSource(data=dict(x1=[], y1=[]))
m1_spans = ColumnDataSource(data=dict(x1=[]))
p = figure(x_axis_type="datetime", title="", sizing_mode="fixed", height = 500, width = 1400)
p.line(x ="x1", y="y1", color = 'blue', source=m1_source)
Then I have a for loop that should ideally plot multiple spans, each 'i' will be a separate timestamp:
for i in m1_spans.data['x1']:
p.add_layout(Span(location=i, dimension='height', line_color='red', line_dash='solid', line_width=1))
This is taken from my update() function:
m1_source.data = dict(
x1=machine_1_vals['mpTimestamp'],
y1=machine_1_vals['Value'])
m1_spans.data = dict( x1=ToolsDF.loc[ToolsDF['Value'] == float(vals['Tool_val'])]['Timestamp'])
I have checked this, and m1_spans does successfully return multiple timestamps, so the error shouldn't be here.
The reason I am confused, is because my p.line will successfully update with no issues, but it does have a source parameter, while span does not.
I would be really appreciative for any advice about how to solve this issue.
If I should have supplied more information, I apologize and can update as needed, I just tried to keep it brief for you.
Thanks.
Span objects do not currently have an ability to be "powered" by a ColumnDataSource. Each Span only draws one span, specified by its own location property.
You will need to update the location property individually on each Span object in order to update it. Alternatively, if you absolutely want to be able to drive updates through a CDS, you could look at using a multi_line, segment, or ray glyph instead. Those all have different ways to configure their coordinates, so you'd have to see which is most convenient to your use-case. But they all come with one trade-off, which is that none of them have the full "infinite extent" that a Span supports.
I can’t find any documentation on using paths in gmplot. I came up with this code which gets close, but it connects all of the lines and fills it in with a color - I don't want that.
I simply want a path like this:
http://imgur.com/3iaC2NQ
But, what I’m getting is this:
http://imgur.com/ypXEqof
Heres’s my code:
pathlon = -117.2974695,-117.2980671,-117.2984607,-117.2979182,-117.2974082,-117.2966604,-117.2977518,-117.2987498,-117.2981491,-117.297708,-117.2972804,-117.2965301,-117.2979485,-117.2975028,-117.2980506,-117.2982983,-117.2976609,-117.2970861,-117.2969908,-117.2981731,-117.2987695,-117.2981146,-117.2976909,-117.2969674,-117.2969299,-117.298394
pathlat = 33.27172039,33.27197757,33.27217535,33.27225324,33.27218351,33.27233921,33.27242614,33.27248971,33.27268346,33.27265944,33.27263664,33.27279608,33.27281652,33.27194103,33.27176546,33.27224514,33.27222714,33.27208829,33.27237357,33.27243373,33.27262189,33.27268296,33.27265933,33.27262125,33.27282274,33.27283925
gmap = gmplot.GoogleMapPlotter(pathlat[0],pathlon[0],18)
gmap.polygon(pathlat,pathlon,edge_color="cyan", edge_width=5, face_color=None, face_alpha=None, clickable = False)
gmap.draw('map.html')
Hoping someone can point me in the right direction.
Thanks in advance!
Since you are using polygon, this is the expected consequence. You should use plot method.
Please see following part;
pathlon = -117.2974695,-117.2980671,-117.2984607,-117.2979182,-117.2974082,-117.2966604,-117.2977518,-117.2987498,-117.2981491,-117.297708,-117.2972804,-117.2965301,-117.2979485,-117.2975028,-117.2980506,-117.2982983,-117.2976609,-117.2970861,-117.2969908,-117.2981731,-117.2987695,-117.2981146,-117.2976909,-117.2969674,-117.2969299,-117.298394
pathlat = 33.27172039,33.27197757,33.27217535,33.27225324,33.27218351,33.27233921,33.27242614,33.27248971,33.27268346,33.27265944,33.27263664,33.27279608,33.27281652,33.27194103,33.27176546,33.27224514,33.27222714,33.27208829,33.27237357,33.27243373,33.27262189,33.27268296,33.27265933,33.27262125,33.27282274,33.27283925
gmap = gmplot.GoogleMapPlotter(pathlat[0],pathlon[0],18)
gmap.plot(pathlat,pathlon,'cornflowerblue', edge_width=10)
gmap.draw('map.html')
I am trying to work out a way to change between two sets of labels on a map. I have a map with zip codes that are labeled and I want to be able to output two maps: one with the zip code label (ZIP) and one with a value from a field I have joined to the data (called chrlabel). The goal is to have one map showing data for each zip code, and a second map giving the zip code as reference.
My initial attempt which I can't get working looks like this:
1) I added a second data frame to my map and add a new layer that contains two polygons with names "zip" and "chrlabel".
2) I use this frame to enable data driven pages and then I hide it behind the primary frame (I don't want to see those polygons, I just want to use them to control the data driven pages).
3) In the zip code labels I tried to write a VBScript expression like this pseudo-code:
test = "
If test = "zip" then
label = ZIP
else
label = CHRLABEL
endif
This does not work because the dynamic text does not resolve to the page name in the VBScript.
Is there some way to call the page name in VBScript so that I can make this work?
If not, is there another way to do this?
My other thought is to add another field to the layer that gets filled with a one or a zero. Then I could replace the if-then test condition with if NewField = 1.
Then I would just need to write a script that updates all the NewFields for the zipcode features when the data driven page advances to the second page. Is there a way to trigger a script (python or other) when a data driven page changes?
Thanks
8 months too late, but for posterity...
You're making things hard on yourself - it would be much easier to set up a duplicate layer and use different layers, then adjust layer visibility. I'm not familiar with VBScript for this sort of thing, but in Python (using ESRI's library) it would look something like so [python 2.6, ArcMap 10 - sample only, haven't debugged this but I do similar things quite often]:
from arcpy import mapping
## Load the map from disk
mxdFilePath = "C:\\GIS_Maps_Folder\\MyMap.mxd"
mapDoc = mapping.MapDocument(mxdFilePath)
## Load map elements
dataFrame = mapping.ListDataFrames(mapDoc)[0] #assumes you want the first dataframe; you can also search by name
mxdLayers = mapping.ListLayers(dataFrame)
## Adjust layers
for layer in mxdLayers:
if (layer.name == 'zip'):
zip_lyr = layer
elif(layer.name == 'sample_units'):
labels_lyr = layer
## Print zip code map
zip_lyr.visible = True
zip_lyr.showLabels = True
labels_lyr.visible = False
labels_lyr.showLabels = False
zip_path = "C:\\Output_Folder\\Zips.pdf"
mapping.ExportToPDF(mapDoc, zip_path, layers_attributes="NONE", resolution=150)
## Print labels map
zip_lyr.visible = False
zip_lyr.showLabels = False
labels_lyr.visible = True
labels_lyr.showLabels = True
labels_path = "C:\\Output_Folder\\Labels.pdf"
mapping.ExportToPDF(mapDoc, labels_path, layers_attributes="NONE", resolution=150)
## Combine files (if desired)
pdfDoc = mapping.PDFDocumentCreate("C:\\Output_Folder\\Output.pdf"")
pdfDoc.appendPages(zip_path)
pdfDoc.appendPages(labels_path)
pdfDoc.saveAndClose()
As far as the Data Driven Pages go, you can export them all at once or in a loop, and adjust whatever you want, although I'm not sure why you'd need to if you use something similar to the above. The ESRI documentation and examples are actually quite good on this. (You should be able to get to all the other Python documentation pretty easily from that page.)