How to disable automatic label wrapping in matplotlib [duplicate] - python

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

Related

To change values across x & y axis in a plotting [duplicate]

This question already has answers here:
How to set ticks on Fixed Position , matplotlib
(2 answers)
How to force integer tick labels
(4 answers)
How to force matplotlib to show values on x-axis as integers
(2 answers)
In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]
(1 answer)
Closed 1 year ago.
I want to change values across the x- & y-axes in a plot.
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['figure.figsize'] = 7,6
x = range(1,20)
y = list(np.random.rand(19) * 100)
fig = plt.figure()
ax = fig.add_axes([0.01,0.1,1,1])
ax.plot(x,y,marker = 'o')
I want to change x-axis values that are appearing as decimals. Instead, I want to customize it and show values like 2,4,6 .. 18.
Any suggestions will be a kind help.
Thank you

How to plot y-axis to the opposite side? [duplicate]

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

matplotlib: Use fixed number of decimals with scientific notation in tick labels [duplicate]

This question already has answers here:
Displaying first decimal digit in scientific notation in Matplotlib
(3 answers)
Closed 4 years ago.
Consider the y ticks in this small example:
import matplotlib.pyplot as plt
plt.plot([200,400,600,800])
plt.ticklabel_format(axis='y',style='sci',scilimits=(0,0))
plt.show()
Resulting plot:
I would like the y tick labels to have a fixed format with one decimal, i.e., 1.0 instead of 1, etc. How can I do that while keeping the exponent for the scientific notation on top of the plot?
All solutions I've found so far (most using FormatStrFormatter instead of ScalarFormatter) make the exponent repeat on all tick labels, like 1.0e2, 2.0e2, etc., which is not what I want.
Thank you in advance.
I've just found a solution. Here it goes:
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
class ScalarFormatterForceFormat(ScalarFormatter):
def _set_format(self,vmin,vmax): # Override function that finds format to use.
self.format = "%1.1f" # Give format here
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot([200,400,600,800])
yfmt = ScalarFormatterForceFormat()
yfmt.set_powerlimits((0,0))
ax.yaxis.set_major_formatter(yfmt)
plt.show()
It was based on this answer on a different thread.

How to define range of x values in plotting in matplotlib.pyplot? [duplicate]

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.

Plotting system of (implicit) equations in matplotlib [duplicate]

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.

Categories