Polar plot without grid in matplotlib - python

Is there a way to turn of the grid for polar plots in matplotlib? I tried matplotlib.pyplot.rgrids([], []), but it doesn't work.

From your axes instance, call grid(False).
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.grid(False)
r = np.arange(0,1,0.001)
theta = 2*2*np.pi*r
ax.plot(theta,r)
plt.show()

Related

Bothering frame on matplotlib 3D plots

I make 3d plots with matplotlib and I always get a weird frame with a normalized scale around my plot. Where does it come from and how can I get rid of it ?
Here is an example code that drives me to the problem :
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(0,10)
y = np.linspace(0,10)
z = np.linspace(0,10)
# ------------- Figure ---------------
fig, ax = plt.subplots(figsize = (9,6))
ax = fig.gca(projection='3d')
ax.plot(np.sin(x), np.cos(y), z)
plt.show()
And here is the result :
I use plt.subplots() because I want a figure with a 3D and a 2D plot side by side.
You call plt.subplots(...) and this, of course, instantiates an Axes, complete of horizontal and vertical spines, before Matplotlib is informed that you want a 3D enabled Axes.
When you later call plt.gca(...) it's too late…
Simply use
fig, ax = plt.subplots(figsize = (9,6), subplot_kw={"projection" : "3d"})
or
fig = plt.figure(figsize = (9,6))
ax = fig.add_subplot(111, projection='3d')
Addressing OP's comment
Figure.add_subplot is pretty flexible…
fig = plt.figure()
fig.add_subplot(1,5,(1,4), projection='3d')
fig.add_subplot(1,5,5)
fig.tight_layout()
plt.show()

3D scatter with two axes in logscale

I am trying to get a scatter plot with 2 axes (x,z) in a log scale using set_scale but that doesn't work.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x=np.linspace(0,500,10)
y=np.linspace(0,1,10)
z=np.linspace(0,100000,10)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(x, y, z,'o', c='g')
ax.grid()
#ax.set_xscale('log')
#ax.set_zscale('log')
#ax.set_xlim([min(x), max(x)])
#ax.set_zlim([min(z), max(z)])
plt.show()

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()

How do you scale a polygon patch in matplotlib?

In the example below, I create a rectangular patch using matplotlib.patches.Polygon. Is there a way to scale the patch before adding it to the plot?
I've tried using matplotlib.transforms.Affine2D in a variety of ways with no success. As usual, the matplotlib documentation on transformations is woefully insufficient.
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
poly = Polygon( zip(x,y), facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
If by scale you mean multiplication by a factor, you can easily do this via numpy.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
scale = 2
poly = Polygon( np.c_[x,y]*scale, facecolor='red', edgecolor='red', alpha=0.5)
ax.add_patch(poly)
plt.show()
The same can be achieved with a matplotlib.transforms.Affine2D() transform.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.transforms as transforms
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot([-3,3],[-3,3])
x = [-1,0,1,1,0,-1]
y = [1,1,1,-1,-1,-1]
trans = transforms.Affine2D().scale(2) + ax.transData
poly = Polygon( np.c_[x,y], facecolor='red', edgecolor='red', alpha=0.5,
transform=trans)
ax.add_patch(poly)
plt.show()
Although it seems a bit overkill for a simple scaling like this.

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