How to do ylim plot Limits with Holoviews Bokeh Datashader - python

How do I add y-limits (ylim) to a plot created using Holoviews Datashader?
I have tried the hv.Dimension function and also adding ylim=() parameters but it would either be the Holoview that rejects it or the Datashader function that doesn't understand the parameter.
plot_Z1 = datashade(hv.Curve(df).redim(y=hv.Dimension('y', range=(-50,50))))
plot_Z2 = datashade(hv.Curve(df).redim(y=hv.Dimension('y', range=(-50,50))))
plot_Z1.options(width=500) + plot_Z2.options(width=500)
ylim isn't recognized and hv.Dimension has no effect

It would appear that I cannot use ylim and shared_axes together with Holoview datashading. At least not in the sense where shared_axes works properly that it will zoom in/out on all subplots together. If I stick to just Holoviews either it won't apply the ylim or the shared_axes won't zoom in/out on all subplots (only one plot with zoom while others stay still).
The only way I found to get shared_axes working properly together with a ylim parameter is using HVPLOT instead.
plot_1 = df.hvplot(y='Something', width=200, datashade=True)
plot_2 = df.hvplot(y='Something Else', width=200, ylim=(-50, 50), datashade=True)
plot = (plot_1 + plot_2.options(shared_axes=True)).cols(1)
plot

Related

How can I use alpha with seaborn.pointplot? [duplicate]

I want to make a seaborn pointplot that has transparency so that I can clearly see the points located behind others of a different color.
I tried adding "alpha=0.3" to the call to pointplot and also tried the same within a catplot with kind='point'; however, neither of these results in the desired transparency (no error message is produced either).
sns.pointplot(x='aamm', y='posrate', hue='AA:XX', hue_order=[1,0], data=data, dodge=True, palette=palette, alpha=0.3)
I was hoping to get a plot with transparent points, but instead, I got one with normal opaque points. The dodge option doesn't seem to produce any noticeable effect either, in terms of separating overlapping points of different color.
Is there a way to add transparency to a seaborn pointplot or use something else to get a similar effect?
Thank you.
To the extent of my knowledge there is no more an alpha parameter that can be directly set in seaborn.
You can do the following thou:
Sample dataframe
df = pd.DataFrame(np.random.randint(low=0, high=1000, size=(50, 5)))
Plotting
ax = sns.pointplot(x=0, y=1, data=df, dodge=True,plot_kws=dict(alpha=0.3))
plt.setp(ax.collections, alpha=.3) #for the markers
plt.setp(ax.lines, alpha=.3) #for the lines

How do I scale mplfinance graph within Tkinter?

With the help of my post earlier, I have managed to turn my chart into a candlestick one. However I cannot seem to adjust the size of it.
Old chart:
New Chart:
I'd like to resize it to how it was before. I have tried adding the following to rcparams but it had no effect:
"figure.figsize": figure_size,
"savefig.bbox": "tight"
My old relevant code:
figure = plt.Figure(figsize=(0.7 * res_width / 100, 0.45 * res_height / 100), facecolor=BACKGROUND_COLOUR)
ax = figure.add_subplot(111, fc=BACKGROUND_COLOUR)
figure.tight_layout()
figure.subplots_adjust(left=0.05, right=1.0, bottom=0.0, top=1.0)
FigureCanvasTkAgg(figure, self).get_tk_widget().place(relx=0.02, rely=0.27)
My current code:
market_colours = mpf.make_marketcolors(up="g", down="r",
edge=background_colour,
wick=line_colour)
style_dict = {"xtick.color": line_colour,
"ytick.color": line_colour,
"xtick.labelcolor": text_colour,
"ytick.labelcolor": text_colour,
"axes.spines.top": False,
"axes.spines.right": False,
"axes.labelcolor": text_colour}
style = mpf.make_mpf_style(marketcolors=market_colours,
facecolor=background_colour,
edgecolor=line_colour,
figcolor=background_colour,
gridcolor=line_colour,
gridstyle="--",
rc=style_dict)
figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR))
FigureCanvasTkAgg(figure, self).get_tk_widget().place(relx=0.02, rely=0.27)
When trying .pack(expand=True) on the widget it also doesn't work.
Edit:
Thanks to Mr Goldfarb I have got the graph to be bigger, but it still does not fit right. Is there a way I can apply the figure.tight_layout() along with figure.subplots_adjust(left=0.05, right=1.0, bottom=0.0, top=1.0) that I usually add?
You can adjust the figure size with kwarg figsize=(width,height) in your call to mpf.plot(). Instead of:
figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR)
Try
figure, ax = mpf.plot(df, type="candle", returnfig=True, style=style, TEXT_COLOUR,
figsize=(0.7*res_width/100, 0.45*res_height/100) )
Regarding Figure.tight_layout() and Figure.subplots_adjust() ... mplfinance uses matplotlib's Figure.add_axes() to create Axes objects, and apparently, although add_axes() is part of matplotlib, the Axes that are created by add_axes() are incompatible with matplotlib's tight_layout. I do not know if subplots_adjust() will work either (as I've never tried it).
That said, mplfinance implements it's own tight_layout. Just set kwarg tight_layout=True when calling mpf.plot(). Try that first, but if that doesn't satisfy what you are trying to accomplish, then try using the scale_padding kwarg.

Create Legend Label for Quad glyph - Bokeh

I have a Quad plot displaying 2 data-sets. I would like to add a legend to the plot, however I am not sure how to do this with the Quad glyph.
Previous examples have used 'legend' however this is now deprecated, and I've tried using
'legend_label' however this is does not work.
My ultimate goal is to use the legend to interactively display both datasets
# Convert dataframe to column data source
src1 = ColumnDataSource(Merged_Bins)
src2 = ColumnDataSource(Merged_Bins)
#------------------------------------------------------------------------------------------------
# Plot Histogram using Bokeh plotting library
#------------------------------------------------------------------------------------------------
plot = figure(y_range=Range1d(start=0, end=Max_Histogram_Value),sizing_mode="scale_width",width=3000,height= 600,
title= "Histogram Plot",
x_axis_label="Time (ms)",
y_axis_label="Count",toolbar_location = "below")
plot.yaxis.ticker = FixedTicker(ticks=list(tick_vals))
glyph1=Quad(bottom=0, top='Delay1', left='left1',
right='right1', fill_color='#FF7F00',
line_color='black', fill_alpha=0.7,line_alpha=0.5,name="Option 2")
glyph1_plot=plot.add_glyph(src1, glyph1)
glyph2=Quad(bottom=0, top='Delay2', left='left2',
right='right2', fill_color='#616261',
line_color='#616261',line_alpha=0.1, fill_alpha=0.1,name="Original Design")
plot.add_glyph(src2, glyph2)
# Add hover tool for when mouse is over data
hover1 = HoverTool(tooltips=[('Delay Envelope', '#Bin_interval'),('Count', '#Delay1'),('Count Original', '#Delay2')],mode='vline',renderers=[glyph1_plot])
plot.add_tools(hover1)
plot.legend.location = "top_left"
plot.legend.click_policy="hide"
# Set autohide to true to only show the toolbar when mouse is over plot
plot.toolbar.autohide = True
script, div = components(plot)
show(plot)
It works just fine if you use the Figure.quad method instead of manually calling Figure.add_glyph with an explicitly created instance of Quad. All legen_* arguments are parsed by glyph methods of the Figure class - the glyph classes themselves do not use them at all.
from bokeh.io import show
from bokeh.plotting import figure
p = figure()
p.quad(-1, 1, 1, -1, legend_label='Hello')
p.quad(1, 3, 3, 1, color='green', legend_label='there')
show(p)
Alternatively, if you really need the manual approach for some reason, you can also create a legend manually by creating an instance of the Legend class and by adding it to the figure with Figure.add_layout.
Also, on an unrelated note - your plot looks like it was created with vbar instead of quad because all bars seem to have the same width. If so, perhaps using vbar would be simpler in your case.

python matplotlib: how to move the scale to the other side of the axis?

I have this weird thing with the scale of the axis showing out of the figure like:
And what I want to have:
How can I move the scale to the other side of the axis?
x=range(len(ticks))
plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')
plt.xticks(x,ticks,rotation=45)
plt.xlabel('Messung')
plt.ylabel('$\Delta \phi [^\circ]$')
plt.legend()
plt.show()
The tick_params of your axis can be used to control axes label and ticks location. Set direction to in so that they point into the graph.
And here is a great example if you want different y-axis ranges and colours too.
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.tick_params(direction='in', length=6, width=2, colors='r', right=True, labelright='on')
plt.show()
You can use plt.tick_params() to adjust the behaviour of the ticks, documentation can be found here.
For your example you want the ticks to appear inside the figure. Therefore add
plt.tick_params(direction="in")
to your code. Example:
x=range(len(ticks))
plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')
plt.xticks(x,ticks,rotation=45)
plt.xlabel('Messung')
plt.ylabel('$\Delta \phi [^\circ]$')
plt.legend()
plt.tick_params(direction="in") # Set ticks inside the figure
plt.show()
You can get the ticks to appear on the top and right side of the figure too as shown in your second screenshot by adding:
plt.tick_params(direction="in",top="on",right="on")
If you wanted to make all figures in your Python script have this behaviour then you can add the following at the top of your script (this might be of interest):
import matplotlib
matplotlib.rcParams['xtick.direction'] = "in"
matplotlib.rcParams['ytick.direction'] = "in"
This will save you having to call plt.tick_params() for each figure, which is helpful if you generate lots of figures.

How do I set the size of the axes patch so that the plot labels aren't clipped (matplotlib)

I have a graph in which I've set the axis labels to scientific notation using
formatter = mpl.ticker.FormatStrFormatter('%4.2e')
axis2.yaxis.set_major_formatter(formatter)
However, the axes.patch (or whatever is the right way to express the 'canvas' extent of the plot) doesn't adjust so the tick labels and axis label are clipped:
How do I adjust the extent of the axes portion of the plot. Changing the page size (figsize = ...) doesn't do it, since that just scales the overall plot area, resulting in the same clipping problem.
You can use the method tight_layout, which will accommodate the plot in the figure available space.
Example
from pylab import *
f = figure()
f.add_subplot(111)
f.tight_layout()
show()
Hope it helps.
Cheers
Just call fig.tight_layout() (assuming you have a Figure object defined).

Categories

Resources