I would like to add a scale bar to plots of spatstat objects but am not sure how.
Here is a reproducible example:
Plot ppp object called "cells":
data(cells)
plot(cells)
How would I add a scale bar to this plot?
I just Googled 'spatstat scale bar' and the second hit was the help file for yardstick() while the top hit concerned the plot method for the yard stick. The title of the help file is "yardstick: Text, Arrow or Scale Bar in a Diagram". Maybe that is what you are looking for.
E.g:
library(spatstat)
W <- union.owin(owin(c(0,1),c(0,5)), owin(c(0,5),c(4,5)))
X <- runifpoint(100, win = W)
plot(X, main = "")
y <- yardstick(3,1,4,1, txt = "1 unit")
plot(y, add = TRUE)
Created on 2018-11-11 by the reprex package (v0.2.1)
Related
I want to place the y-label in the left upper corner in line with the points on the coordinate axis. The label should replace the axes ticks in that area.
Here is an example from a paper that I read:
I found a lot of information on how to move the label to the top left corner. However, how do I remove the ticks and move the label closer to the axes?
The best solution that I found:
# style
plt.figure(figsize=(8,8))
sns.set_style("white")
sns.set_context("paper", font_scale=1.6)
# plot
sn_dis1 = sns.displot(idx_pix_count_pd, hue='Dataset', x="Size", fill=True, palette='bwr', kind="hist", bins=500)
# y-achses
axes = sn_dis1.axes.flatten()
axes[0].set_ylabel("Count", fontsize=16)
axes[0].yaxis.set_label_coords(.0, .9)
The approach proposed by Trenton (Edit: Who delete his comment to my original question) did not work for me as the text is horizontal vertically. But I learned something from it and am sure that you somehow can make it work like that.
# style
plt.figure(figsize=(8,8))
sns.set_style("white")
sns.set_context("paper", font_scale=1.6)
# plot
sn_dis1 = sns.displot(idx_pix_count_pd, hue='Dataset', x="Size", fill=True, palette='bwr', kind="hist", bins=500)
# y-achses
ticks = range(8)
labels= list(range(7)) + ['Density']
axes = sn_dis1.axes.flatten()
axes[0].set_yticklabels(labels)
# do not show default label
axes[0].get_yaxis().get_label().set_visible(False)
I'm in the final portion of my project and we just need to make a graph showing the number of shows and movies with their rating trend. I have 4 different bar graph items and I was wondering if there's a way to add labels to each individual bar? For example here's one of the bars
y4 = [fourth]
x4 = [4.33]
bg4 = pg.BarGraphItem(x=x4, height=y4, width=0.2, brush='red', setLabel="Shows Down")
bg4.setX(0)
bg4.setY(0)
You could use a group of TextItem and set the bar as their parent:
text = pg.TextItem('Some bar', angle=90, color='#ffff00')
# reparent the item and make it a child of the bar
text.setParentItem(bg4)
# set the position to the x of the bar
text.setX(x4[0])
# use the x as left of the text and the y as vertical middle (referenced
# to the text orientation)
text.setAnchor(QPointF(0, .5))
Note that you don't need to create individual bars as long as they are part of the same group and share the same colors: just add the x and height as lists of their positions and values.
I tried to reproduce the solution from: How do I work with images in Bokeh (Python) , but it doesn't work. For that, I find an image on the net and put it in place of the 'url' field but the plot is just blank! From the original solution bokeh ask me to add up w and h params which I suppose are the width and height of the pic. Also I dropped x_range and y_range within figure() to wipe out the horizontal and vertical lines of the plot.
from bokeh.plotting import figure, show, output_notebook
output_notebook()
p = figure()
p.image_url( url=[ "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png"],
x=1, y=1, w=253, h=409)
show( p)
Anyone could tell me what's going on ?
Bokeh can't auto-range ImageURL it seems. So if there are no other glyphs, you need to provide explicit ranges. Additionlly, the default anchor is upper_left IIRC so it may be that our image is rendering off-canvas and you don't realize it. The code below works with Bokeh 0.12.5:
from bokeh.plotting import figure, show, output_file
output_file("foo.html")
p = figure(x_range=(0,500), y_range=(0,500))
p.image_url( url=[ "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png"],
x=1, y=1, w=253, h=409, anchor="bottom_left")
show(p)
Without the anchor set, the image plots blow the plot region (have to pan to see it)
I wrote some Excel VBA code that generates a scatterplot and changes a few properties of the chart. (Code is below for reference.) The code moves slowly through tasks like deleting the chart legend, removing horizontal/vertical gridlines, and changing the X and Y series. Excel's timer gives me the following duration for each task:
insert scatterplot: 0.01171875
delete series: 0
plot x vs y: 0.55859375
delete legend: 0.5703125
delete chart title: 0.66015625
remove grid: 1.3046875
format axes: 0
overall: 3.11328125
Removing the grid, changing the title, plotting the X and Y series, and deleting the legend seem to take a long time. I've googled for alternative ways to write the code, but haven't been able to find anything useful. The code works entirely as expected, except for the slow speed. Any ideas as to what's causing the bad performance, and how I can speed this up? Thanks in advance.
EDIT: I've already turned off screen updating while working with the chart. The chart is generated/formatted while a userform is open, if that makes any difference.
Here is the relevant snippet of code:
With ActiveChart
'Delete all series currently in plot
Do While .FullSeriesCollection.Count > 0
.FullSeriesCollection(1).Delete
Loop
'Plot Actual (Y) vs. Inverse Distribution (X)
.SeriesCollection.NewSeries
.FullSeriesCollection(1).XValues = "=" & tempSheetName & "!$C:$C"
.FullSeriesCollection(1).Values = "=" & tempSheetName & "!$A:$A"
'Delete legend
.Legend.Delete
'Delete chart title
.SetElement (msoElementChartTitleNone)
'Remove gridlines
.SetElement (msoElementPrimaryValueGridLinesNone)
.SetElement (msoElementPrimaryCategoryGridLinesNone)
'Format axes
Dim xAxis As Axis, yAxis As Axis
Set xAxis = .Axes(xlCategory)
Set yAxis = .Axes(xlValue)
With yAxis
'Title y axis "actual"
.HasTitle = True
.AxisTitle.Caption = "Actual"
'Add tick marks
.MajorTickMark = xlOutside
End With
With xAxis
'Title x axis by dist type
.HasTitle = True
.AxisTitle.Caption = dist.getDistType
'Add tick marks
.MajorTickMark = xlOutside
End With
End With
Without the data and machine specifics it can be hard to say why this is slow, although here are some alternatives to some of the code you have.
The first and foremost thing I'd change is not to Activate the chart. If you are creating the chart through code, do so but set it to a variable, eg Set wcChart = ThisWorkbook.Charts.Add. Then change With ActiveChart to With wcChart.
Also, delete the FullSeriesCollection and then delete the chart title, remove the gridlines and change the axes before filling up the new data. The chart manipulation should be quicker with less data in the chart. Be careful here though because changing aspects of the chart in different orders can produce different outputs (as an example the layout of a legend).
You fill the new FullSeriesCollection with the entire columns of A and C, specify the exact range of the data rather than the whole column.
Other changes to try, I'm not saying these will work but they are worth a shot if you haven't tried. Instead of checking for a FullSeriesCollection each time:
Do While .FullSeriesCollection.Count > 0
.FullSeriesCollection(1).Delete
Loop
The following may be quicker:
For ii = .FullSeriesCollection.Count To 1 Step -1
.FullSeriesCollection(ii).Delete
Next ii
Also, instead of .SetElement for the Chart title and Gridlines I use the following:
'You have to set the title to 'True' before it'll work with 'False'. Go figure.
.HasTitle = True
.HasTitle = False
.HasMajorGridlines = False
.HasMinorGridlines = False
I have several questions to do with handling some data in R:
I am using this statement: detailsTable <- read.table(file=commandArgs()[6], header=TRUE, col.names=c("a", "b", "c", "d", "e")) and it seems that the table is not being loaded correctly... but if I specify the path of the file I am loading excplicitly then all goes well. What am I doing wrong?
I plot the data contained in that table mentioned above. How do I save the plot (eg: plot.savePDF("plot.pdf")) to a PDF file?
How could I redirect the output of, for example, cor(detailsTable$a, detailsTable$b) to a file? and how do I write a simple string to a file. eg: "Correlation of the data: " + cor(...)
How do I plot the line of best fit on an existing plot?
All of this is in R.
Many thanks to anyone who can help,
ExtremeCoder
I plot the data contained in that table mentioned above. How do I save the plot (eg: plot.savePDF("plot.pdf")) to a PDF file?
pdf("filename.pdf")
plot(...)
dev.off()
How could I redirect the output of, for example, cor(detailsTable$a, detailsTable$b) to a file? and how do I write a simple string to a file. eg: "Correlation of the data: " + cor(...)
check the write.table manual page (?write.table)
How do I plot the line of best fit on an existing plot?
x <- 1:10
y <- 2 * x + runif(10)
plot (x, y, pch=20)
fit <- glm(y~x)
coefs <- coef(fit)
abline(coefs, lwd=2, col='red')
# Or also, without finding the coefficients
abline(fit, lwd=2, col='red')
You can redirect output using sink().
How to save the plot you're producing depends on which plotting system you're using. Assuming it's base graphics, you need to start a pdf graphics device, then plot to it.
pdf(file = "path/file.pdf", width = 5, height = 5)
...
#plotting commands here
...
dev.off()