This question already has answers here:
Is it possible to plot implicit equations using Matplotlib?
(6 answers)
Closed 7 years ago.
First off, I'm new to python and matplotlib. I need to plot several systems of implicit equations in one figure.
The equations are in form of:
3x+2y=1
Is there an easy way to plot these, other than first making the equations explicit (i.e. y=...)?
import numpy as np
import matplotlib.pyplot as plt
# Note the order of y,x.
y,x=np.ogrid[-5:5:100j,-5:5:100j]
plt.contour(x.ravel(),y.ravel(),3*x+2*y,[1])
plt.show()
You can use contour() to do implicit plots in two space dimensions:
x = numpy.linspace(-2., 2.)
y = numpy.linspace(-2., 2.)[:, None]
contour(x, y.ravel(), 3*x + 2*y, [1])
In 3 dimensions, I suggest using Mayavi instead of matplotlib.
Related
This question already has answers here:
Prevent scientific notation
(1 answer)
How to prevent numbers being changed to exponential form in a plot
(6 answers)
turn off scientific notation for matplotlib [duplicate]
(1 answer)
matplotlib remove axis label offset by default
(2 answers)
Closed 1 year ago.
The image speaks for itself:
I want matplotlib to explicitly print the full length y-labels (8 digits total with 6 decimals after the point). But it keeps splitting them into the bias (which can be seen in the top left corner) and the remainder.
I tried disabling the autoscale and setting manual ylims, doesn't help.
You can retrieve the default y-ticks using plt.gca().get_yticks() and then use plt.gca().set_yticklabels to set them to your desired format (documentation for .set_yticklabels is here and documentation for .gca is here).
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
## reproduce your curve
x = np.linspace(31.5,34,500)
y = 3.31*(10**-5)*x**2 - 2.22*(10**-3)*x - 3.68*10**1
plt.scatter(x,y,marker='.')
## retreieve and set yticks
yticks = plt.gca().get_yticks()
plt.gca().yaxis.set_major_locator(mticker.FixedLocator(yticks))
plt.gca().set_yticklabels([f"{y:.6f}" for y in yticks])
plt.show()
This question already has answers here:
matplotlib y-axis label on right side
(4 answers)
Closed 2 years ago.
I have this chart below:
I would want the y-axis for the lower subplot to be plotted to the opposite side since that would make more sense. Is there a method for this? The ax.invert_yaxis() simply inverts the labels.
Note: For the curious, I simply used .invert_xaxis() to plot inverted bars.
I guess, what you are looking for is
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
of an axis object.
So with #meTchaikovsky's MVE code, you'll get
import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(1,10,10)
y0 = np.random.randint(0,30,size=10)
fig,ax = plt.subplots(nrows=2,ncols=1,figsize=(8,6))
ax[1].set_xlim(0,30)
ax[0].barh(x,y0,color='violet')
ax[0].set_ylabel("Y-Axis")
ax[1].set_xlim(30,0)
ax[1].barh(x,y0,color='deepskyblue')
ax[1].yaxis.set_ticks_position("right")
ax[1].yaxis.set_label_position("right")
ax[1].set_ylabel("Y-Axis")
plt.show()
This question already has an answer here:
Hexbin plot in PairGrid with Seaborn
(1 answer)
Closed 3 years ago.
I would like to do a pairplot for all columns in my DataFrame; however instead of the scatter plot, I would like to produce hexbin plots (so I can better see density of points).
sns.pairplot doesn't have this option, I was wondering how it would be possible?
Paitplot plots two kinds of plot in a single canvas for all possible pairs of variable
Distribution Plot which is diagonal plots. You can set it by passing argument diag_kind : {‘auto’, ‘hist’, ‘kde’}, optional
Scatter Plots which are off-diagonal plots. Set it by using kind : {‘scatter’, ‘reg’}, optional
See here for more information.
The kind of plot which you want, you need to use sns.jointplot. You can use it as follows as suggested by #cripcate in the comment.
import numpy as np
import seaborn as sns
%matplotlib inline #extra attention at this line
sns.set(style="ticks")
rs = np.random.RandomState(11)
x = rs.gamma(2, size=1000)
y = -.5 * x + rs.normal(size=1000)
sns.jointplot(x, y, kind="hex", color="#4CB391")
This question already has answers here:
matplotlib create broken axis in subplot
(1 answer)
remove part of a plot in matplotlib
(1 answer)
Closed 5 years ago.
I want to define range of x values appeared in plot in python. For example, here is my code:
import matplotlib.pyplot as plt
plt.figure(13)
ax1=plt.subplot(111)
x=[i for i in range(100)]
y=[i**2 for i in x]
ax1.plot(x,y)
However, I want to see in the graph x ranges from 0 to 60 and 80 to 100. How, I can do it in matplot. Actual, problem is more complicated, just removing data doesn't appear to be good solution. I want to change plot.
I tried ax1.set_xlim([0,60]) but want to see from 80 to 100 also.
This question already has answers here:
How to remove relative shift in matplotlib axis
(2 answers)
Closed 8 years ago.
I am using Python and Pyplot to produce a plot. The y axis of the plot is generated automatically, and works fine.
If the range is small however, the y axis will produce values such as:
0.00005,
0.00010,
0.00015,
0.00020,
0.00025,
0.00030,
and then at the top of the axis, will say:
+1.543e-1
I would prefer that it just explicitly shows the values:
0.15435
0.15440
0.15445
0.15450
0.15455
0.15460
Could someone tell me how to do this please?
Alright, I think I found the total solution:
Here is a quick plot that recreates your problem:
plot((0.15435,0.15440,0.15445,0.15450,0.15455,0.15460),(0.15435,0.15440,0.15445,0.15450,0.15455,0.15460))
The following code (similar to how it was as shown here) should adjust the ticker like you want:
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
gca().yaxis.set_major_formatter(y_formatter)