How to set logarithmic scale and axis limits in HistogramLUTItem in pyqtgraph - pyqtgraph

I'm using pyqtgraph for a live view of a camera acquisition program. Most of the times my images are composed of a lot of background noise and a signal of just a few pixels with higher intensity. For that reason, the part of the HistogramLUTItem that corresponds to the actual signal looks like a thin line and the noise is big next to it. Being able to plot the logarithm of the data would make the data to stand up more.
Is this possible?
I'm currently creating the histogram this way:
imagewidget = pg.GraphicsLayoutWidget()
self.p1 = imagewidget.addPlot()
self.img = pg.ImageItem()
self.p1.addItem(self.img)
self.p1.getViewBox().setAspectLocked(True)
self.hist = pg.HistogramLUTItem()
self.hist.setImageItem(self.img)
self.hist.autoHistogramRange = False
imagewidget.addItem(self.hist)
Doing self.hist.axis.setLogMode(True) didn't work as it affected the x-axis of the histogram instead of the y-axis.
And finally, I would also like to be able to limit the accesible range in the x-axis of the histogram. How can this be done?
Cheers!

Ok, I finally figured out. In case someone wonders, I solved it by adding these two lines:
self.hist.plot.setLogMode(False, True)
self.hist.vb.setLimits(yMin=0, yMax=16000)

Related

Bokeh Server Plot - How to make new values appear in the center with a fixed axis range

I want to create a plot that shows the live metering data I am getting from an electricity meter.
I already figured out how to have a plot in bokeh, that updates every x seconds with new values, but now I want to have the new values always be at a fixed point in the plot, while the range of the axis does not increase.
I fixed the range by adding x_range=[0, 10] to the figure, however that plot is running out of the screen and I have to manually follow it.
How do I change it so it follows it automatically?
Is that even possible in bokeh or should I be using something different for my project?
Use a default DataRange1d range (i.e. do not set the range to a fixed interval) and then set the follow property on the range. You can also set follow_interval to specify how far back the range should trail the latests data.
p.x_range.follow = "end"
p.x_range.follow_interval = 100
For a complete demonstration see the OHLC ticker example.

Labelling X and Y axis in Dymola plot

I have drawn a plot using "plotArrays" function in Dymola. I would like to label X-axis in meters(Ideally, I need it to be flexible so that I can later change it to millimeters, micrometers, etc..). I want to label Y-axis similarly in volts which I have done already using
plotArrays(x_axis,phie,legend=names,units=fill("V",size(phie,2)));
and I can read the values in volts and also change to mV etc.. However, its not 'visible' in Y-axis as you can see in the plot below. So, How can I label the axes separately?
Thanks a lot!
I don't know any more efficient possibility than this:
createPlot(id=1, erase=false, grid=true, leftTitleType=2, leftTitle="myLabelY", bottomTitleType=2, bottomTitle="myLabelX")
with:
id being the number shown in the original plot
erase=false to ensure that the content is not modified
grid=true (re-)enabling the grid
*TitleType=2 saying that there is a custom title
*Title being the string to put there
This will result in the plot being resized to the default size. You can use plotSetup() to get the current setup, including position, which you can then pass (manually) to the createPlot() command to result in the original size again.
Not very elegant, but I don't know any other possibility...

Population Pyramid in Plotrix freezes r [duplicate]

I am trying make a pyramid plot with R. The I found a example code in the internet that does what I want. The problem is that I am not working with small numbers as in the example. My plot has values of 3,000,000 to 12,000,000 but only 10 bars per side. Never the less it takes for ever create the plot with the larger numbers and output pdf file is about 800mb of size.
pyramid.plot(x,y,labels=groups,main="Performance",lxcol=mcol,rxcol=fcol,gap=0.5,show.values=TRUE)
Why is the performance so bad? Shouldn't get scaled automatically?
Update:
pdf(file='figure1.pdf')
library(plotrix)
x <-c(3105000,3400001,4168780,2842764,3543116,4224601,4222222,6432105,9222222,12345596)
y <-c(3105000,3400001,4168780,2842764,3543116,4224601,4222222,6432105,9222222,12345596)
groups <-c("g1","g2","g3","g4","g5","g6","g7","g8","g9","g11")
pyramid.plot(x,y,labels=groups,main="Performance",gap=0.5,show.values=TRUE)
dev.off()
Both the export to pdf as well as the plotting screen takes multiple minutes.
Internally, pyramid.plot is trying to do some stuff to finagle the axes accounting for the gap in the middle: if you do debug(pyramid.plot) and step through line-by-line you find where the problem is:
if (is.null(laxlab)) {
laxlab <- seq(xlim[1] - gap, 0, by = -1)
axis(1, at = -xlim[1]:-gap, labels = laxlab)
}
in other words, pyramid.plot is trying to make an axis with ticks every 1 (!) unit.
Something like this works OK:
pyramid.plot(x,y,labels=groups,
main="Performance",gap=5e5,show.values=TRUE,
laxlab=seq(0,1e7,by=1e6),raxlab=seq(0,1e7,by=1e6))
there are a few other vestiges of the fact that pyramid.plot was designed for demographic plots ... you might write to the package maintainer and ask him to think about generalizing the design of the axes a little bit ...

add data points to existing plot in R

I try to receive the data from a sensor from time to time and plot it in real time. That means the length of the dataset is not know before hand. And need to adjust the range of the graph dynamically.
I tried the following
plot(1,10, xlim=range(0,10), ylim=range(0,10), type='n')
points(1,data[1])
points(2,data[2])
But once the number of dots is beyond the range of x axis (10 in this case), the data points are out of the range. How to adjust the range accordingly?
Just issue a new plot command with an expanded range. On modern computers the time taken to recreate the plot is small and you generally will not see a delay. Any other approach will essentially do the same thing, clear the current plot and create a new plot.
The ggplot2 and lattice packages have ways of constructing a plot and updating the plot, but when the updated plot is shown it is redrawn from scratch.
There is a zoomplot function in the TeachingDemos package which will allow you to change the range of a plot, but it also will just redraw the plot from scratch (and due to changes in R 3.0.0 it is not currently working, so if you wanted to use it you would need to go back to R 2.15 or before, or wait for it to be fixed).
You can't adjust the range dynamically (sometimes Excel is better). However, you can keep track of what you've plotted, and redo the plot when you've reached the limit. You could also just make a new plot every time you get more data, which would be a way of faking a dynamic update.

Broken axis in Google charts

Is there any way to create a break in my vertical scale on the Google charts api?
I have a couple of dozen data points all about 600-2000 on the y-axis except for one value which is almost 300,000; this makes all the smaller data points nearly unreadable. I need to represent all this data and a logarithmic scale is not an option.
Simple answer: no, it is not possible.
Breaking axes is (generally) frowned upon in the visualization community and therefore isn't supported most of the time in various software.
If you want to be tricky, you can create a function to find outliers, and then move them to a second series in your data. Plot that series on the second axis, and have it with a different color. This says, "This figure is different and does not fit" which brings added attention to it, while still allowing the rest of the data to be seen in the same scale.
Personally I would just cut off the graph at an arbitrary value, set the value of that point to the maximum value, and add a tooltip saying, "Outlier: 300,000" or whatever it is. This will allow people to see the other numbers, but show that this number itself is an outlier without coloring it differently or removing it from the single series.
Either way is doable.
You need use a log scale. It's a vAxis and hAxis attribute. The supported values are:
log: Conventional logarithm scale
mirrorLog: Logarithm scale that allows 0 values
var options = {
vAxis: {
scaleType: 'mirrorLog',
}
};
var data = {};//your data
chart.draw(data, options);

Resources