I have generated a bunch of data, and want to plot how they are distributed using matplotlib. How should I proceed?
Thank you for answers
hist is a nice way to see a distribution. See here for details.
Related
I want to make a phase plot like this https://en.wikipedia.org/wiki/Phase_portrait from an non-linear time series in R, Any ideas?
Thank you
you haven't given many details, but I suggest you look at the package phaseR.
I use it to draw isoclines and a flowfield of predator-prey models.
My graphs look like this (right one):
I have a vector which I have made a density function for. It is a curve with multiple peaks, and I want to perform a Gaussian deconvolution in order to represent my density curve with multiple Gaussian curves. I am hoping there is a package that will allow me to do this, but I haven't found anything that has worked for me so far. I tried to follow the example given here (https://www.r-bloggers.com/fitting-mixture-distributions-with-the-r-package-mixtools/) but the vector I am working with is ~400 mb and won't play nicely with the mixtools package tools. I need something that will work with the size of the datasets I am working with or be able to work with the density curve directly. Any help appreciated, thank you!
I do not have sample data but I am happy to have it explained with the faithful dataset in R, I know I can find the density of faithful$waiting with density(faithful$waiting) and it plots just fine, but the deconvolution has me stuck.
I have a plotting problem with curves when using mixtools
Using the following R code
require(mixtools)
x <- c(rnorm(10000,8,2),rnorm(10000,18,5))
xMix <- normalmixEM(x, lambda=NULL, mu=NULL, sigma=NULL)
plot(xMix, which = 2, nclass=25)
I get a nice histogram, with the 2 normal curves estimated from the model superimposed.
The problem is with the default colours (i.e. red and green), which I need to change for a publication to be black and grey.
One way I thought to doing this was first to produce the histogram
hist(xMix$x, freq=FALSE, nclass=25)
and then add the lines using the "curve" function.
....... but I lost my way, and couldn't solve it
I would be grateful for any pointers or the actual solution
thanks
PS. Note that there is an alternative work-around to this problem using ggplot:
Any suggestions for how I can plot mixEM type data using ggplot2
but for various reasons I need to keep using the base graphics
You can also edit the colours directly using the col2 argument in the mixtools plotting function
For example
plot(xMix, which = 2, nclass=25, col2=c("dimgrey","black"))
giving the problem a bit more thought, I managed to rephrase the problem and ask the question in a much more direct way
Using user-defined functions within "curve" function in R graphics
this delivered two nice solutions of how to use the "curve" function to draw normal distributions produced by the mixture modelling.
the overall answer therefore is to use the "hist" function to draw a histogram of the raw data, then the "curve" function (incorporating the sdnorm function) to draw each normal distribution. This gives total control of the colours (and potentially any other graphic parameter).
And not to forget - this is where I got the code for the sdnorm function - and other useful insights
Any suggestions for how I can plot mixEM type data using ggplot2
Thanks as always to StackOverflow and the contributors who provide such helpful advice.
I hope i do all well, because I am new to this forum.
I try to plot a distribution function of discontinuous values like a cube
Can I do in r anything like the picture on this website?
http://www.dietrichgrude.de/stochastik/zufvar_vertfkt.gif
Thanks and greetings
You can use plot.stepfun function. See examples of it with
example(plot.stepfun)
There is histfit function in Matlab would plot histogram and fit the distribution by bin values.
The distribution's parameters have to be estimated.
How to implement histfit in r? I searched for a long time, but it has no lucky.
This post have mentioned this before, but there is no preferable solution. The sn package seems support several distribution, not so much.
I explore the data with hist function, the histogram shows gamma distribution in gerneral.
But if I add up bins and show it again, the graph will show more details, and gamma distribution fails.
fitdistr would fail to find parameters also.
so I want to fit the data just using the coarse data from histogram. This is the question, thank you for your help.
The fitdistr function in the MASS package can be used to find parameters for a given distribution (including gamma). The function density and the logspline package (and others) can be used to estimate the density function of the data without assuming a specific distribution.
The lines and curve functions can be used to add an estimated density curve to a plotted histogram (use prob=TRUE when creating the histogram).
If you want to compare your data to a specific distribution then tools like qqplots (qqplot function or others) or visual tests (vis.test in the TeachingDemos package) will probably be better than a histogram and density plot.
I have to answer it myself, package 'bda' could fit the binned data in several distributions, however it could only binning data by rounding.