My plot does not perform correctly
for k in range(0,len(j)):
fig = px.scatter(X_reduced_df, x='EXTRACTION_DATE_SAMPLE', y=[j[k]], title = "Größe " + str(k) +": " + j[k],
color= 'anomaly_class_IF', hover_data=['score_IF','delta', 'omicron', 'NH4-N (titr.)(W)_Zusatzparameter-ESRI',
'P ges. filtr. (photom.)(W) mA_Zusatzparameter-ESRI',
'BSB5 (mit Verd) DIN 1899_Zusatzparameter-ESRI',
'N org. (Ber. TKN)(W)_Zusatzparameter-ESRI'
])
fig.show()
the result is
why do i get this? the date is not shown correctly, and as a consequence, the plot is empty.
i tried the plot without the loop and it works just fine. what can i do to fix this?
Related
New user with Altair, trying to sort NFL player names by the "Player COUNT" field. The data is pre-aggregated to show each playername only once, with the count associated to it.
Sorting the y axis which is 'Player Name' based on the count in -x works fine until I add color, which i want to be a discrete dimension. Any idea what needs to be done to prevent color from overriding the sort on Y?
import altair as alt
import pandas as pd
source = pd.read_csv("C:/Users/abcdef/Documents/user_players.csv", encoding = "ISO-8859-1")
print(source.head())
bars = alt.Chart(source).mark_bar().encode(
x=alt.X('Player COUNT:Q'),#,scale=alt.Scale(domain=(1,5),clamp=True)),
y=alt.Y('Player Name:N',sort='-x')
#y=alt.Y('Player Name:N',sort=alt.SortField(field="sort_order",op="distinct",order='descending')),
,color='Ownership Percentile:O'
).transform_filter(
alt.FieldLTEPredicate(field='player_row', lte=50)
)
text = bars.mark_text(
align='left',
baseline='middle',
dx=3 # Nudges text to right so it doesn't appear on top of the bar
).encode(
text='Player COUNT:Q'
)
(bars + text).properties(width =400,height=900)#.interactive()
This looks like a bug in the Vega-Lite renderer. You can work around it by using an EncodingSortField with an explicit op in place of the -x shorthand:
bars = alt.Chart(source).mark_bar().encode(
x=alt.X('Player COUNT:Q'),#,scale=alt.Scale(domain=(1,5),clamp=True)),
y=alt.Y('Player Name:N',sort=alt.EncodingSortField(field="Player COUNT", order='descending', op='max')),
color='Ownership Percentile:O'
).transform_filter(
alt.FieldLTEPredicate(field='player_row', lte=50)
)
The spacing on the y axis label get all wrong when printing with print
The code to reproduce the issue is:
data1 = [1:10:100]
data2 = [100:10:200]
file = "plottest.png"
[at ho ht] = plotyy(data1,data1,data2,data2);
ylabel(at(1),"Concentration of O_2 (mol/m^3)");
print(gcf,file);
I'm using the qt graphics tool.
Output to figure window (and also desired result):
What gets printed to PNG file:
One workaround could be using the latex interpreter as
ylabel(at(1),"Concentration of $O_2$ (mol/m^3)", 'Interpreter', 'latex');
Dear friends I'm trying binarise the image, it’ll look best if I cut the black parts out. To avoid any funny edge effects I’ll crop the pattern to a circle, and so our ‘mask’ is drawn below, along with the relevant snippet of Matlab
P = imread('Pattern.png');
P = 1-im2bw(P); % Keep black parts
Nmin = min(size(P));
% Crop into square, then circle
P = P(1:Nmin, 1:Nmin);
[xg, yg] = meshgrid(1:Nmin, 1:Nmin);
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
% Create a small border
P = padarray(P, [1 1], 0);
But for some reason I'm having issues with line 7,
Error: File: Try1.m Line: 7 Column: 42
Unbalanced or unexpected parenthesis or bracket.
please help me...
edit:
here is explanation
I assume that you have copied this code from a website. The reason is the following line:
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
Notice the >? (which is what #beaker mentioned in their comment) If you check the original code on the website where you found it, there is probably a > instead of > right?
The reason is that in HTML, the > sign is displayed by writing > in the HTML source code. When the website is shown in your browser, it will convert > to the > sign, but apparently when copy-pasting the code, it apparently copied the HTML source code and not the rendered sign.
MATLAB of course can't handle HTML source code, so you have to replace > by > and your code works fine:
P((xg - Nmin/2).^2 + (yg - Nmin/2).^2 > 0.99*0.25*Nmin^2) = 0;
Is there a way to make a rasterGrob-object partly transparent, so to add an alpha-factor to it? I'm using a logo as a watermark within a ggplot2 plot by inserting a png-image (as rasterGrob) by annotation_custom. However, unlike with annotate, the alpha option does not work here, so I guess the image has to be changed in advance.
As a simple example based on what baptiste suggests in his blog, so far I'm doing it this way:
img.path <- readPNG("logo.png")
pngob <- rasterGrob(img.path)
qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(pngob, xmin=6.8, xmax=Inf, ymin=1, ymax=Inf) +
geom_point()
The example above works perfectly.
However, typing dim(pngob) into the console returns NULL. Thus, the suggestion below on how to set an alpha-value does not work:
m <- pngob
w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1])
This returns the error Error in m[, , 1]: wrong number of dimensions
Straight from the ggplot2 blog by #baptiste. You can adjust alpha when you create w.
library(png)
library(gridExtra)
m <- readPNG(system.file("img", "Rlogo.png", package="png"), FALSE)
w <- matrix(rgb(m[,,1],m[,,2],m[,,3], m[,,4] * 0.2), nrow=dim(m)[1]) #0.2 is alpha
qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rpatternGrob(motif=w, motif.width = unit(1, "cm"))) +
geom_point()
Or if you want to have a single image:
qplot(1:10, rnorm(10), geom = "blank") +
annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf,
rasterGrob(w)) +
geom_point()
I am encountering an odd problem. I am able to create and save pdf file using R/ggplot2 and view them while the R Console is running. As soon as I exit the R console, Preview on Mac OS X will no longer display the PDF. I have been able to save .png files w/o problem, but for reasons beyond my control, I need to save in pdf files. The code I am using to save is as follows:
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined, aes(x=sequenceName, fill=factor(subClass))) + geom_bar()
ggsave(pdfFile)
Has anyone encountered a similar problem? If so, what do I need to do to fix it?
Thank you very much for your time.
The problem is that you don't close the pdf() device with dev.off()
dat <- data.frame(A = 1:10, B = runif(10))
require(ggplot2)
pdf("ggplot1.pdf")
ggplot(dat, aes(x = A, y = B)) + geom_point()
dev.off()
That works, as does:
ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("ggplot1.pdf")
But don't mix the two.
It is in the R FAQ, you need a print() around your call to ggplot() -- and you need to close the plotting device with dev.off() as well, ie try
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined,aes(x=sequenceName,fill=factor(subClass)))
+ geom_bar()
dev.off()
Edit: I was half-right on the dev.off(), apparently the print() isn;t needed. Gavin's answer has more.
The following plot
pdf("test.pdf")
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
dev.off()
works in the console but not in a function or when you source this from a file.
myfunc <- function() {
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
}
pdf("test.pdf")
myfunc()
dev.off()
Will produce a corrupt pdf file and the way to fix it us use
print(p)
within a function.
In a console. "p" is automatically printed but not in a function or when you source the file.
You can also change the filename of your pdf plot within ggsave if you want to call it something other than "ggplot1" or whatever concise object name you chose; just give the filename first and then tell it which plot you're referring to, for example:
a <- ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("Structure.pdf",plot=a)