confusion matrix plot is unable to show values within plot - data-science

I'm trying to plot confusion matrix, but the plot seems unable to show values at the center.
Code :
plt.figure(figsize=(4,2))
sns.heatmap(cm, annot=True, annot_kws={"size":20},
cmap='Blues', square=True, fmt='.2f')
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.title('Confusion matrix for:\n{}'.format(model.__class__.__name__));
Output :
How to have the values 0.45, 0.94, 0.06 and 0.55 to be at the center of square box for better readability ?

The figure size appears to be too small - 4x2. Also, you are making square=True, the size is not right.
As I dont have the same model, I tried it with another model and this is the code and output
plt.figure(figsize=(4,4))
sns.heatmap((metrics.confusion_matrix(y_test, svmGridtest_predict)),annot=True,
annot_kws={"size":20}, fmt='.2f', square=True, cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actuals',rotation=0)
plt.title('Confusion matrix for:\n{}'.format('SVM'))
OUTPUT figure here...

Related

Trying to plot two lines on mathplotlib

I am tyring two plot who lines but cannot get the graph to scale properly
#plot2
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(totalReturnComp["Date"], totalReturnComp["S&P compounded growth"], linewidth=2, color='b')
ax2=ax1.twiny()
ax2.plot(sp_trr["month"], sp_trr["1 MEqual Weight"], linewidth=2, color='g')
ax2.legend(['3 Month Incremental Weight'],frameon=False)
ax1.legend(['S&P Index compounded growth'],frameon=False)
plt.title('3 Month Incremental Weight vs S&P Index')
Plot
plot

Seaborn: how to make a Log base 2 axis in a small multiples plot?

I'm doing a small multiples plot with seaborn using relplot:
g = sns.relplot(data=df,
kind='scatter',
col='mycol', row='arow',
x='a', y='b',
hue='c',
legend=False,
alpha=.5)
I can easily tranform the axis for a log scale in base 10:
g.set(xscale="log")
g.set(yscale="log")
If I were ploting a simple plot with matplotlib I'd be able to use a log scale in base 2:
ax.set_xscale('log', basex=2)
ax.set_yscale('log', basey=2)
But how do I make a Log2 plot in Seaborn?
Just discovered how to do it using a global function:
plt.xscale('log', basex=2)
plt.yscale('log', basey=2)

axes and labeling not in plot white area

I am trying to make a plot in a Colaboratory notebook. I am using following code:
# Plot data
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(12,8))
plt.title("Charleston", size=24)
plt.xlabel("Year", size=20)
plt.ylabel("High Tide Floods (days/year)", size=20)
plt.plot(HTF_yr.index, HTF_yr.numdays, color='black',marker ='o')
plt.show()
Result (below) displays axes and labels in a grey area. How do I include them in the white area, i.e. the figure itself?

Scatter plot without x-axis

I am trying to visualize some data and have built a scatter plot with this code -
sns.regplot(y="Calls", x="clientid", data=Drop)
This is the output -
I don't want it to consider the x-axis. I just want to see how the data lie w.r.t y-axis. Is there a way to do that?
As #iayork suggested, you can see the distribution of your points with a striplot or a swarmplot (you could also combine them with a violinplot). If you need to move the points closer to the y-axis, you can simply adjust the size of the figure so that the width is small compared to the height (here i'm doing 2 subplots on a 4x5 in figure, which means that each plot is roughly 2x5 in).
fig, (ax1,ax2) = plt.subplots(1,2, figsize=(4,5))
sns.stripplot(d, orient='vert', ax=ax1)
sns.swarmplot(d, orient='vert', ax=ax2)
plt.tight_layout()
However, I'm going to suggest that maybe you want to use distplot instead. This function is specifically created to show the distribution of you data. Here i'm plotting the KDE of the data, as well as the "rugplot", which shows the position of the points along the y-axis:
fig = plt.figure()
sns.distplot(d, kde=True, vertical=True, rug=True, hist=False, kde_kws=dict(shade=True), rug_kws=dict(lw=2, color='orange'))

Plot error bars (percentile)

I'm quite new to python and I need some help. I would like to plot errorbars equivalent to 1sigma standard deviations on my plot as the 16th and 84th percentile values of the distributions. I tried with (using matplotlib):
err=np.std(x)
but it just gives me the standard deviations.
Thanks.
If you want vertical error bars
ax = plt.gca()
ax.errorbar(x, y, yerr=np.vstack([error_low, error_high]))
plt.draw()
where error_low and error_high are 1D sequences of the same length an x and y. The error bars are drawn at y[i] - error_low[i] and y[i] + error_high[i].
matplotlib just draws what you tell it to, it is your job to provide the semantics.
errorbar documentation