Bokeh logarithmic scale for Bar chart - python

I know that I can do logarithmic scales with bokeh using the plotting API:
p = figure(
tools="pan,box_zoom,reset,previewsave",
y_axis_type="log", y_range=[0.001, 10**22], title="log axis example",
x_axis_label='sections', y_axis_label='particles'
)
However, I can't figure out how to get this to apply to high level charts such as Bokeh.charts.Bar. In general I'm having a lot of trouble grokking what to relationship is between a Chart and a figure. Can anyone point me to some documentation on this or explain how to modify things which are only exposed through figure and have them affect my Chart.

I am specifically going to update the documentation describing the different Bokeh APIs this week, but for now, the three Bokeh APIs in increasing order of "level":
models interface: lowest level API, base serialization layer, must put everything together everything manually
glyphs interface (bokeh.plotting): mid-level API, easily create plots/figures centered around visual glyphs with attributes tied to data
charts interface (bokeh.charts): high level API for canned/schematic statistical charts, e.g. "BoxPlot" and "Histogram".
There is no particular relation between figure and the various chart functions, except that they both produces subclasses of Plot as output.
I am not sure it is currently possible to add a log axis to the Bar plot in "charts" interface (that would be a reasonable feature to add). However it would be simple to make a boxplot "by hand" using the middle "glyphs" interface using rect or quad glyphs. Here is a quick example:
from bokeh.plotting import figure, output_file, show
output_file("bars.html")
p = figure(title="log bar example", y_axis_type="log")
p.quad(
bottom=0, top=[10**5, 10**8, 10**3],
left=[0, 2, 4], right=[1,3,5]
)
show(p)

Related

Add labels to real-time signal plotting with Vispy

I am not familiar enough with Vispy. I did adapt this example for my use case, but I don't know how to modify it further to include the missing features.
I am trying to plot in real-time N-channels of data at the same scale. Using pyqtgraph, the interface looks like this:
And with vispy:
My goal is to match both backends, step by step. The difference for now are:
The order in Vispy is reversed (plot at the top on Vispy is the plot at the bottom on pyqtgraph)
Lack channel names
Lack Y-axis label
Lack X-Axis and X-Axis label
Lack of headroom top/bottom
I do not know how to solve any of those, how to further improve this backend. Any tips, guidance towards the correct, best function to use for this would be very helpful.
I was looking into visual.text, but the positioning seemed difficult. I did not know how to match the label in front of one of the plot.

MACD plot in python similar to tradingview figure

Here is an example of MACD plot using python.
https://code.luasoftware.com/tutorials/algo-trading/python-mplfinance-plot-yfinance-candle-chart-moving-average-macd-and-volume/
I'd like the result (including colors and crossover points, etc.) to be the same as that of tradingview.com
https://www.tradingview.com/script/NDH8bJow-MACD-Colors-Signals/
Could anybody show the python so that it is the same as that of tradingview.com?
Could anybody show the python so that it is the same as that of tradingview.com?
I don't think anyone is going to want to write your code for you.
However what I would recommend is that you work through the mplfinance tutorials on Customizing the Appearance of Your Plots and especially teach yourself about mplfinance styles which shows how to modify the overall color scheme of your plot. See also mplfinance style sheets reference.
For example, to achieve a color scheme similar to trading view you could use the "market colors" from style 'yahoo' and the combine that with mplfinance style 'nightclouds'. Something like this:
mc = mpf.make_marketcolors(base_mpf_style='yahoo')
s = mpf.make_mpf_style(base_mpf_style='nightclouds',marketcolors=mc)
Then when you call mpf.plot() specify kwarg style=s
hth

Data visualization in python (matplotlib) [duplicate]

I'm not really new to matplotlib and I'm deeply ashamed to admit I have always used it as a tool for getting a solution as quick and easy as possible. So I know how to get basic plots, subplots and stuff and have quite a few code which gets reused from time to time...but I have no "deep(er) knowledge" of matplotlib.
Recently I thought I should change this and work myself through some tutorials. However, I am still confused about matplotlibs plt, fig(ure) and ax(arr). What is really the difference?
In most cases, for some "quick'n'dirty' plotting I see people using just pyplot as plt and directly plot with plt.plot. Since I am having multiple stuff to plot quite often, I frequently use f, axarr = plt.subplots()...but most times you see only code putting data into the axarr and ignoring the figure f.
So, my question is: what is a clean way to work with matplotlib? When to use plt only, what is or what should a figure be used for? Should subplots just containing data? Or is it valid and good practice to everything like styling, clearing a plot, ..., inside of subplots?
I hope this is not to wide-ranging. Basically I am asking for some advice for the true purposes of plt <-> fig <-> ax(arr) (and when/how to use them properly).
Tutorials would also be welcome. The matplotlib documentation is rather confusing to me. When one searches something really specific, like rescaling a legend, different plot markers and colors and so on the official documentation is really precise but rather general information is not that good in my opinion. Too much different examples, no real explanations of the purposes...looks more or less like a big listing of all possible API methods and arguments.
pyplot is the 'scripting' level API in matplotlib (its highest level API to do a lot with matplotlib). It allows you to use matplotlib using a procedural interface in a similar way as you can do it with Matlab. pyplot has a notion of 'current figure' and 'current axes' that all the functions delegate to (#tacaswell dixit). So, when you use the functions available on the module pyplot you are plotting to the 'current figure' and 'current axes'.
If you want 'fine-grain' control of where/what your are plotting then you should use an object oriented API using instances of Figure and Axes.
Functions available in pyplot have an equivalent method in the Axes.
From the repo anatomy of matplotlib:
The Figure is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures and Figures can contain multiple Axes.
But...
Most plotting occurs on an Axes. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we'll set up an Axes with a call to subplot (which places Axes on a regular grid), so in most cases, Axes and Subplot are synonymous.
Each Axes has an XAxis and a YAxis. These contain the ticks, tick locations, labels, etc.
If you want to know the anatomy of a plot you can visit this link.
I think that this tutorial explains well the basic notions of the object hierarchy of matplotlib like Figure and Axes, as well as the notion of current figure and current Axes.
If you want a quick answer: There is the Figure object which is the container that wraps multiple Axes(which is different from axis) which also contains smaller objects like legends, line, tick marks ... as shown in this image taken from matplotlib documentation
So when we do
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> type(fig)
<class 'matplotlib.figure.Figure'>
>>> type(ax)
<class 'matplotlib.axes._subplots.AxesSubplot'>
We have created a Figure object and an Axes object that is contained in that figure.
pyplot is matlab like API for those who are familiar with matlab and want to make quick and dirty plots
figure is object-oriented API for those who doesn't care about matlab style plotting
So you can use either one but perhaps not both together.

Is it possible to combine a stream with no parameters with standard linked streams when declaring a HoloViews DynamicMap?

What I'm trying to do is make an interactive scatter plot where I can control which columns of a DataFrame are on X and Y axes and then select a subset of data using lasso or something similar. Because of the dataset size I have to use datashader.
I tried to declare the DynamicMap as:
dmap = hv.DynamicMap(selector.make_view, kdims=[], streams=[selector, RangeX(), RangeY(), Stream.define('Next')()])
and have a custom callback on the lasso which would select desired rows of data, create the visual representation and update the plot with dmap.event().
So that doesn't seem to work. If I select something, the plot gets updated only when I pan or zoom or change axes selection. VIDEO
If I leave only Stream.define('Next')():
dmap = hv.DynamicMap(selector.make_view, kdims=[], streams=[Stream.define('Next')()])
then lasso updates the plot, but I loose everything else including the ability to zoom. VIDEO
I hope this question makes sense. If needed, I've pushed the notebook here.

Multiple bars of the same data on the same y-axis in Bokeh

I'm using Bokeh's hbar_stack() method in order to make a stacked horizontal bar chart.
In Matthew Rocklin's post here, the very first chart plots how long tasks take on different worker cores. It plots "read-block" and "pandas_read_text."
I was wondering how you could get multiple bars of a phase (ex. "read-block") onto the same y-axis when using hbar_stack(). Any general advice behind how this chart is created would also be helpful.

Categories

Resources