Matplotlib - Draw H and V line by specifying X or Y value on a plot - matplotlib

I was wondering today about how finding a specific value on a plot and drawing the right line that goes with. I used to do that on an old chart library, and I was wondering that perhaps this functionnality exist but I don't know how to find it.
The result should look like this: https://miro.medium.com/max/1070/1*Ckhi9soE9Lx2lIf9tPVLMQ.png
To provide some context, I'm doing a PCA over my data, and I would like to point out some thresholds at 97.5, 99 and 99.5% of explained cumuled variance.
Have a great day!
EDIT:
See Answer

As solved by ImportanceOfBeingErnest, here is the code:
whole_pca = PCA().fit(np.array(inputs['Scale'].tolist()))
cumul = np.cumsum(np.round(whole_pca.explained_variance_ratio_, decimals=3)*100)
over_95 = np.argmax(cumul>95)
over_99 = np.argmax(cumul>99)
over_995 = np.argmax(cumul>99.5)
plt.plot(cumul)
plt.plot([0,over_95,over_95], [95,95,0])
plt.plot([0,over_99,over_99], [99,99,0])
plt.plot([0,over_995,over_995], [99.5,99.5,0])
plt.xlim(left=0)
plt.ylim(bottom=80)
plt.ylabel('% Variance Explained')
plt.xlabel('# of Features')
plt.title('PCA Analysis')
Result in:
Thank you!

Related

Setting Qtile Margins Dynamically Through Keyboard Input

I'm looking to set up keybindings to increase/decrease gaps and margins in Qtile similar to what the following does in i3-gaps:
bindsym $mod+equal gaps inner current plus 5
bindsym $mod+minus gaps inner current minus 5
bindsym $mod+Shift+equal gaps outer current plus 5
bindsym $mod+Shift+minus gaps outer current minus 5
I can somewhat get the equivalent of outer-gaps to work with the following code:
def increase_gap(qtile):
qtile.screens[0].top.size = screens[0].top.size+5
qtile.screens[0].right.size = screens[0].top.size+5
qtile.screens[0].left.size = screens[0].top.size+5
#margs = screens[0].bottom.margin
screens[0].bottom.margin[0] = screens[0].bottom.margin[0]+5
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
def decrease_gap(qtile):
qtile.screens[0].top.size = max(screens[0].top.size-5, 0)
qtile.screens[0].right.size = max(screens[0].top.size-5, 0)
qtile.screens[0].left.size = max(screens[0].top.size-5, 0)
#margs = screens[0].bottom.margin
screens[0].bottom.margin[0] = max(screens[0].bottom.margin[0]-5, 0)
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
Key([mod, "shift"], "equal", lazy.function(increase_gap), desc="Increase gap"),
Key([mod, "shift"], "minus", lazy.function(decrease_gap), desc="Decrease gap"),
I'm not sure this is the right way to do things, though. I'm not sure if this is supposed to be manipulated this way. I'm not really sure that the cmd_resize() function is what I should be using, however from trial and error it's what I've found to work. I'm not sure why the screen[0].bottom.size=20 (20 is the size of my bottom bar. I know I shouldn't hardcode, but I'm trying to produce a proof of concept before I clean the code) is needed, but the bar starts floating if I don't have that there. Finally, increasing and decreasing the gap gets close, but not quite to the original configuration. The gaps look slightly different than the original. So, I'm not sure this is the right way to accomplish this, and I could use the advice.
Secondly, though that gets close to accomplishing what I want on the outer gaps, I have not been able to make any headway to getting the inner gaps to work. I initially tried changing the margin parameter of a layout, and when that didn't work I tried simply initializing a new layout and replacing the old one as posted below, but neither approach worked.
def column_increase_margin(qtile):
current_margin = current_margin + 5
layouts[0] = layout.Columns(border_focus_stack='#d75f5f', margin=current_margin, border_width=0)
screens[0].bottom.size=20
qtile.screens[0].cmd_resize()
I've tried going through the code here, but it's a big project and I'm struggling to make heads or tails from it.
Any advice would be appreciated.
If I understand what you want, you can add the following to the Layout class in libqtile/layout/base.py:
def cmd_increase_margin(self):
self.margin += 10
self.group.layout_all()
def cmd_decrease_margin(self):
new_margin = self.margin - 10
if new_margin < 0:
new_margin = 0
self.margin = new_margin
self.group.layout_all()
You can then add some keys in your config.py to increase and decrease margins. e.g.
KeyChord([mod], "m", [
Key([], "Up", lazy.layout.increase_margin()),
Key([], "Down", lazy.layout.decrease_margin())
],
mode="Margins"
),
I'm new to qtile, so maybe there is something wrong with the above approach, but it seems to work.

A value is trying to be set on a copy of a slice from a DataFrame - don't understand ist

I know this topic has been discussed a lot and I am so sorry, that I stil dont't find the sulution, even the difference between a view and a copy is easy to understand (in other languages)
def hole_aktienkurse_und_berechne_hist_einstandspreis(index, start_date, end_date):
df_history = pdr.get_data_yahoo(symbols=index, start=start_date, end=end_date)
df_history['HistEK'] = df_history['Adj Close']
df_only_trd_index = df_group_trade.loc[index].copy()
for i_hst, r_hst in df_history.iterrows():
df_bis = df_only_trd_index[(df_only_trd_index['DateClose']<=i_hst) & (df_only_trd_index['OpenPos']==0)].copy()
# here comes the part what causes the trouble:
df_history.loc[i_hst]['HistEK'] = df_history.loc[i_hst]['Adj Close'] - df_bis['Total'].sum()/100.0
return df_history
I think I tried nearly everithing, but I don't get it. python is not easy when it comes to this topic.
When you have to specify bow index and column in .loc you have to put all together otherwise the annoying message relative to views appears.
df_history.loc[i_hst, 'HistEK'] = df_history.loc[i_hst, 'Adj Close'] - df_bis['Total'].sum()/100.0
Look the examples here

What is the definition of "Error surface" in HEVC

I'm reading a paper about sub-pixel motion estimation optimization algorithm in HEVC;
while all the proposed concepts are based on "Modeling the Error Surface" in the search range(search window)during algorithm;
Does anybody by any chance know the definition of "Error Surface" here?
And what I'm lookin for is definitely not this: Freeform surface modelling.
Thanks.
By the way, the paper's link is here.
The picture below (from the Geneva meeting in January this year) shows the
Integer samples (shaded blocks with upper-case letters) and fractional sample positions (un-shaded blocks with lower-case letters) for quarter sample luma interpolation
The following calculations are needed for the quarter-sample-interpolation:
a0,0 = ( −A−3,0 + 4 * A−2,0 − 10 * A−1,0 + 58 * A0,0 +  17 * A1,0 − 5 * A2,0 + A3,0 )  >>  shift1 (8‑292)
b0,0 = ( −A−3,0 + 4 * A−2,0 − 11 * A−1,0 + 40 * A0,0 +  40 * A1,0 − 11 * A2,0 + 4 * A3,0 − A4,0 )  >>  shift1 (8‑293)
c0,0 = ( A−2,0 − 5 * A−1,0 + 17 * A0,0 +  58 * A1,0 − 10 * A2,0 + 4 * A3,0 − A4,0 )  >>  shift1 (8‑294)
d0,0 = ( −A0,−3 + 4 * A0,−2 − 10 * A0,−1 + 58 * A0,0 +  17 * A0,1 − 5 * A0,2 + A0,3 )  >>  shift1 (8‑295)
h0,0 = ( −A0,−3 + 4 * A0,−2 − 11 * A0,−1 + 40 * A0,0 +  40 * A0,1 − 11 * A0,2 + 4 * A0,3 − A0,4 )  >>  shift1 (8‑296)
n0,0 = ( A0,−2 − 5 * A0,−1 + 17 * A0,0 +  58 * A0,1 − 10 * A0,2 + 4 * A0,3 − A0,4 )  >>  shift1 (8‑297)
– The samples labelled e0,0, i0,0, p0,0, f0,0, j0,0, q0,0, g0,0, k0,0, and r0,0
are derived by applying an 8-tap filter to the samples a0,i, b0,i and c0,i with
i = −3..4 in the vertical direction as follows:
e0,0 = ( −a0,−3 + 4 * a0,−2 − 10 * a0,−1 + 58 * a0,0 +  17 * a0,1 − 5 * a0,2 + a0,3 )  >>  shift2 (8‑298)
i0,0 = ( −a0,−3 + 4 * a0,−2 − 11 * a0,−1 + 40 * a0,0 +  40 * a0,1 − 11 * a0,2 + 4 * a0,3 − a0,4 )  >>  shift2 (8‑299)
p0,0 = ( a0,−2 − 5 * a0,−1 + 17 * a0,0 +  58 * a0,1 − 10 * a0,2 + 4 * a0,3 − a0,4 )  >>  shift2 (8‑300)
f0,0 = ( −b0,−3 + 4 * b0,−2 − 10 * b0,−1 + 58 * b0,0 +  17 * b0,1 − 5 * b0,2 + b0,3 )  >>  shift2 (8‑301)
j0,0 = ( −b0,−3 + 4 * b0,−2 − 11 * b0,−1 + 40 * b0,0 +  40 * b0,1 − 11 * b0,2 + 4 * b0,3 − b0,4 )  >>  shift2 (8‑302)
q0,0 = ( b0,−2 − 5 * b0,−1 + 17 * b0,0 +  58 * b0,1 − 10 * b0,2 + 4 * b0,3 − b0,4 )  >>  shift2 (8‑303)
g0,0 = ( −c0,−3 + 4 * c0,−2 − 10 * c0,−1 + 58 * c0,0 +  17 * c0,1 − 5 * c0,2 + c0,3 )  >>  shift2 (8‑304)
k0,0 = ( −c0,−3 + 4 * c0,−2 − 11 * c0,−1 + 40 * c0,0 +  40 * c0,1 − 11 * c0,2 + 4 * c0,3 − c0,4 )  >>  shift2 (8‑305)
r0,0 = ( c0,−2 − 5 * c0,−1 + 17 * c0,0 +  58 * c0,1 − 10 * c0,2 + 4 * c0,3 − c0,4 )  >>  shift2 (8‑306)
Quite a mouthful as you can see...
The paper you are referring to error surface is probably the difference between the pixel-values calculated using the method proposed in the standard and the second-order-function proposed in the paper. Hope it helps :-)

MATLAB: Resizing a figure properly

I have a figure that I would like to resize and afterwards print as a PDF.
Using something like
set(hFig, 'PaperUnits', 'centimeters')
set(hFig, 'PaperSize', [x_B x_H]);
works as long as I do not resize the figure too drastically. If I reduce the height then at some points the xlabel moves out of the figure. I have searched a lot but only found an solution to manually resize the underlying axes-object
scalefactor = 0.96;
movefactor = 0.82;
hAx = get(gcf,'CurrentAxes');
g = get(hAx,'Position');
% 1=left, 2=bottom, 3=width, 4=height
g(2) = g(2) + (1-movefactor)/2*g(4);
g(4) = scalefactor*g(4);
set(hAx,'Position',g);
I do not like this approach since I have to manually adjust the two factors.
Before printing I set the 'interpreter' to 'latex' of all text-objects (if that is of concern).
Printing is achieved using
print(hFig, '-dpdf', '-loose', 'test.pdf');
I hoped to loosen the bounding box by using '-loose'. Any help is highly appreciated!
edit:
It seems that really the interpreter (none, tex, latex) plays a role in this. I got inspired by this post here (http://stackoverflow.com/questions/5150802/how-to-save-plot-into-pdf-without-large-margin-around) and came up with this solution:
tightInset = get(gca, 'TightInset');
position(1) = tightInset(1);
position(3) = 1 - tightInset(1) - tightInset(3);
if strcmpi(x_Interpreter,'latex')
position(2) = tightInset(2)+ 1*tightInset(4);
position(4) = 1 - tightInset(2) - 2*tightInset(4);
else
position(2) = tightInset(2)+ 0*tightInset(4);
position(4) = 1 - tightInset(2) - 1*tightInset(4);
end
set(gca, 'Position', position);
This may not solve your problem completely (it may just help clean up your code), but I found the fig code in the file exchange to be helpful: it lets you easily set the exact size of figures without bordering white space.

PChart - Show a fixed number of lablels in X-Axis

How to show only a fixed number of labels in X-Axis ??
I have tried "LabelSkip", but I think it works only with an interval and not with fixed number of labels.
Here is a print-screen of my chart:
Are you using pChart 1 or pChart2 ?
This can be achived in pChart 1 using setFixedScale
To draw a scale with a maximum value of 10 with 5 points use the following command before drawing the scale
$Graph->setFixedScale(0,10,5);
I know it has been a while since this was asked, but it may help someone:
$maxXLabels = 5; // How many labels on-screen?
$labelSkip = floor( count( $timestamp ) / $maxXLabels ); // how many should we skip?
$myPicture->drawScale(array("LabelSkip"=>$labelSkip));
I have used
"LabelSkip"=>(count($series)/10)
to have 10 labels on the X axis
Works fine for me
Joel Deutscher's answer worked for me. I would have up voted it, but I do not have enough stackoverflow reputation for that.
It works exactly as he said: Chart Width / MinDivHeight = Number of labels on the chart.
Here's my code
$scaleSettings = array("DrawXLines"=>FALSE,"Mode"=>SCALE_MODE_START0,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM, "MinDivHeight" => 50);
$pchart->chart->drawScale($scaleSettings);