I am using the following code to create a polar plot of the sinus.
import numpy as np
import matplotlib.pyplot as plt
theta = np.arange(0, 2*np.pi, .01)[1:]
plt.polar(theta, sin(theta))
plt.show()
which produces:
but I want to plot it symmetrically, like this:
How can I get the result I want?
The matplotlib polar allows for negative radius. So, if you want the symmetric plot you need to plot the absolute value of sin:
polar(theta, abs(sin(theta)))
Anon, you need to plot the opposite of sin(theta):
plt.polar(theta, sin(theta))
plt.polar(theta, -sin(theta))
Related
I am trying to create a bar range plot with a temporal x-axis with matplotlib. As an example see the following :
As far as I see, Matplotlib doesn't directly support this kind of plot.
What is the best way to achieve this?
Maybe its possible to adjust a boxplot or a fill_between plot?
just pass the bottom parameter to bar, e.g:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y_bot = np.linspace(30, 50, 10)
y_dif = np.linspace(10, 5, 10)
plt.bar(x, y_dif, bottom=y_bot)
I cannot understand how to use fill_betweenx() in matplotlib. How it is different from fill_between()? After reading the documentation of fill_betweenx() I tried to implement it:
x=np.linspace(0,2*3.14,50)
y=np.sin(x)
plt.figure(figsize=(10,5))
plt.fill_betweenx(y,2,3,color='b')
plt.plot(x,y)
As per my understanding, it should have filled the sine curve between x=2 and x=3 with a blue color, but I got:
Can anyone explain to me why it wasn't filled?
It seems you want to fill the sine curve, e.g. between y=0 and the sine. You may limit this fill to a range of x coordinates using where.
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,2*3.14,50)
y=np.sin(x)
plt.fill_between(x,y,0,where=(x>2) & (x<=3),color='b')
plt.plot(x,y)
In contrast you would use fill_betweenx if you wanted to fill between a curve in x direction. E.g.
plt.fill_betweenx(x,y,where=(x>2) & (x<=3), color='b')
plt.plot(y,x)
I can't figure out how to get the axis labels in my Seaborn Joint plot to not display with absolute values (rather than with an offset). I know I can do this in matplotlib with
plt.ticklabel_format(useOffset=False)
But how do I get it to work with this example
import numpy as np
import pandas as pd
import seaborn as sns
sns.set(style="white")
# Generate a random correlated bivariate dataset
rs = np.random.RandomState(5)
mean = [0, 0]
cov = [(1, .5), (.5, 1)]
x1, x2 = rs.multivariate_normal(mean, cov, 500).T
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")
# Show the joint distribution using kernel density estimation
g = sns.jointplot(x1, x2, kind="kde", size=7, space=0, xlim=(0.995, 1.005))
Any suggestions would be greatly appreaciated.
Thanks for your suggestion, #ImportanceofbeingErnest; however that still hasn't solved the problem. Here is a screenshot of the plot, I want the x-axis to look like the y-axis in terms of axis labelling. The offsets disappear if I make the x range larger, but for my dataset that doesn't really work.
My suggestion would be to add import matplotlib.pyplot as plt at the beginning of the script and plt.ticklabel_format(useOffset=False) at the end.
Due to the jointplot creating several axes, plt.ticklabel_format(useOffset=False) will only affect the last of them.
An easy solution is to use
plt.rcParams['axes.formatter.useoffset'] = False
just after the imports. This will turn the offset use off for the complete script.
I have a 64x360 Matrix of values belonging to radial and azimuthal coordinates. I want to visualize them in two plots: a cartesian and a polar plot.
I visualized the heatmap in cartesian coordinates using imshow():
import numpy as np
import matplotlib.pyplot as plt
P=np.loadtxt('Pdata.csv')
print np.shape(P)
plt.imshow(P)
plt.xlabel('radius')
plt.ylabel('theta')
plt.show()
This gives me the desired plot:
The same plot in polar coordinates was also pretty straigh forward using pcolor():
r=np.arange(0,np.shape(P)[1],1)
t=np.arange(0,np.shape(P)[0],1)
R,T = np.meshgrid(r,t)
fig = plt.figure()
ax = fig.add_subplot(111, polar = True)
ax.pcolor(T,R,P)
plt.show()
However, I am not really satisfied with the result:
The resolution of the plot seems to be pretty limited so that it's not possible to distinguish between angles with higher intensity and lower intensity, as it is in the cartesian plot. The whole solid angle seems to be divided into six or seven "cake wedges" only. Is there an easy and pythonic way to enhance the angular resolution?
Ok, I found out something. It works with:
t = np.radians(np.linspace(0, np.shape(P)[0],np.shape(P)[0]))
r = np.arange(0, np.shape(P)[1], 1)
Just as seen here: Polar contour plot in matplotlib - best (modern) way to do it?
Is there a simple way to get log transformed counts when plotting a two dimensional histogram in matplotlib? Unlike the pyplot.hist method, the pyplot.hist2d method does not seem to have a log parameter.
Currently I'm doing the following:
import numpy as np
import matplotlib as mpl
import matplotlib.pylab as plt
matrix, *opt = np.histogram2d(x, y)
img = plt.imshow(matrix, norm = mpl.colors.LogNorm(), cmap = mpl.cm.gray,
interpolation="None")
Which plots the expected histogram, but the axis labels show the indices of the bins and thus not the expected value.
It's kind of embarrassing, but the answer to my question is actually in the docstring of the corresponding code:
Notes
-----
Rendering the histogram with a logarithmic color scale is
accomplished by passing a :class:`colors.LogNorm` instance to
the *norm* keyword argument. Likewise, power-law normalization
(similar in effect to gamma correction) can be accomplished with
:class:`colors.PowerNorm`.
So this works:
import matplotlib as mpl
import matplotlib.pylab as plt
par = plt.hist2d(x, y, norm=mpl.colors.LogNorm(), cmap=mpl.cm.gray)