Python pyplot x-axis label rotation - python

I am trying to rotate the xaxis labels but the xticks function below has no effect and the labels overwrite each other
import matplotlib.pyplot as plt
import seaborn as sns
corrmat = X.corr()
plt.xticks(rotation=90)
plt.figure(figsize=(15,16))
ax = sns.heatmap(corrmat, vmin=0, vmax=1)
ax.xaxis.tick_top()
After using suggested code changes: I get the following but I still want to increase the size of the heatmap

setp looks to be the way to go with pyplot (inspiration from this answer). This works for me:
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np; np.random.seed(0)
data = np.random.rand(10, 12)
ax = sns.heatmap(data)
ax.xaxis.tick_top()
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.show()
Obviously I don't have your data, hence the numpy random data, but otherwise the effect is as required:

Related

Matplotlib/Seaborn: how to plot a rugplot on the top edge of x-axis?

Suppose I draw a plot using the code below. How to plot the rug part on the top edge of x-axis?
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.distplot(np.random.normal(0, 0.1, 100), rug=True, hist=False)
plt.show()
The seaborn.rugplot creates a LineCollection with the length of the lines being defined in axes coordinates. Those are always the same, such that the plot does not change if you invert the axes.
You can create your own LineCollection from the data though. The advantage compared to using bars is that the linewidth is in points and therefore no lines will be lost independend of the data range.
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import seaborn as sns
def upper_rugplot(data, height=.05, ax=None, **kwargs):
from matplotlib.collections import LineCollection
ax = ax or plt.gca()
kwargs.setdefault("linewidth", 1)
segs = np.stack((np.c_[data, data],
np.c_[np.ones_like(data), np.ones_like(data)-height]),
axis=-1)
lc = LineCollection(segs, transform=ax.get_xaxis_transform(), **kwargs)
ax.add_collection(lc)
fig, ax = plt.subplots()
data = np.random.normal(0, 0.1, 100)
sns.distplot(data, rug=False, hist=False, ax=ax)
upper_rugplot(data, ax=ax)
plt.show()
Rugs are just thin lines at the data points. Yo can think of them as thin bars. That being said, you can have a following work around: Plot distplot without rugs and then create a twin x-axis and plot a bar chart with thin bars. Following is a working answer:
import numpy as np; np.random.seed(21)
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots()
data = np.random.normal(0, 0.1, 100)
sns.distplot(data, rug=False, hist=False, ax=ax)
ax1 = ax.twinx()
ax1.bar(data, height=ax.get_ylim()[1]/10, width=0.001)
ax1.set_ylim(ax.get_ylim())
ax1.invert_yaxis()
ax1.set_yticks([])
plt.show()

Seaborn, change font size of the colorbar

I wanted to change the font size for a heatmap colorbar.
The following is my code:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
I was able to change the tick labels with plt.tick_params(axis='both', labelsize=20). However, the colorbar font size does not change.
Is there a way to do that?
You can use matplotlib.axes.Axes.tick_params with labelsize.
For example, your plot with labelsize 20:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
# use matplotlib.colorbar.Colorbar object
cbar = ax.collections[0].colorbar
# here set the labelsize by 20
cbar.ax.tick_params(labelsize=20)
plt.show()
I refered to the following answer:
 - Using matplotlib.colorbar.Colorbar object
 - Setting parameter
You can change the font scale with the seaborn.set() method setting the font_scale param to the scale you want, see more in seaborn documentation.
For example, your plot with scale 3:
import seaborn as sns
import matplotlib.pyplot as plt
from numpy import arange
# here set the scale by 3
sns.set(font_scale=3)
x = arange(25).reshape(5, 5)
cmap = sns.diverging_palette(220, 20, sep=20, as_cmap=True)
ax = sns.heatmap(x, cmap=cmap)
plt.show()
If you set the following, it will increase all text in the graph by a factor of two. However, if you immediately set the tick_params to lower right after, you will be left with just the font size of the colorbar increased.
sns.set(font_scale=2)
sns.heatmap(df, vmin=0, vmax=1, center=0.5)
heatmap.tick_params(labelsize=15)
sns.set(font_scale=1)
Don't forget to set the font_scale back :)

Removing all but one tick on x-axis

I have a graph that I would tick to remove all ticks and their corresponding labels bar the first tick and label on the x-axis. How would I go about this?
import pylab
import numpy as np
import matplotlib.pyplot as plt
a=np.linspace(0,10,10000)
print a
def f(x):
return 1/(1-(x**2)*np.log((1/(x**2)+1)))
b=f(a)
fig, ax = plt.subplots(1)
ax.plot(b,a)
pylab.xlim(0.5, 5)
pylab.ylim(0, 1.5)
fig.show()
you can use ax.set_xticks([1]) to set just one xtick at 1,0.
Also, there's no need to import both pylab and matplotlib.pyplot. The recommended way now is to import matplotlib.pyplot and use all the Axes methods. E.g., you can use ax.set_xlim instead of pylab.xlim.
Here's your full script and output plot:
import numpy as np
import matplotlib.pyplot as plt
a=np.linspace(0,10,10000)
print a
def f(x):
return 1/(1-(x**2)*np.log((1/(x**2)+1)))
b=f(a)
fig, ax = plt.subplots(1)
ax.plot(b,a)
ax.set_xlim(0.5, 5)
ax.set_ylim(0, 1.5)
ax.set_xticks([1])
plt.show()

How do I change the plot size of a regplot in Seaborn?

Something similar to the fig.set_size_inches(18.5, 10.5) of matplotlib.
You can declare fig, ax pair via plt.subplots() first, then set proper size on that figure, and ask sns.regplot to plot on that ax
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# some artificial data
data = np.random.multivariate_normal([0,0], [[1,-0.5],[-0.5,1]], size=100)
# plot
sns.set_style('ticks')
fig, ax = plt.subplots()
fig.set_size_inches(18.5, 10.5)
sns.regplot(data[:,0], data[:,1], ax=ax)
sns.despine()
Or a little bit shorter:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# some artificial data
data = np.random.multivariate_normal([0,0], [[1,-0.5],[-0.5,1]], size=100)
# plot
sns.set_style('ticks')
g = sns.regplot(data[:,0], data[:,1])
g.figure.set_size_inches(18.5, 10.5)
sns.despine()

matplotlib pcolor with modified axis

Goal: I want to modify the axis of the pcolor plot in such a way that the pcolor plot is not changed and not shifted, only the axis!
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
fig, ax = plt.subplots(1,1)
phi = np.random.random([20,10])
xticks = range(10)
yticks = range(10)
ax.pcolor(phi,norm=LogNorm(vmin=10E-3, vmax=10E3))
ax.set_xticks(xticks)
ax.set_xscale("log")
ax.set_yticks(yticks)
fig.show()
failed plot

Categories

Resources