I want to format my ticks to a certain number of significant figures, AND remove the automatic offset. For the latter I am using https://stackoverflow.com/a/6654046/1021819, and for the former I would use https://stackoverflow.com/a/25750438/1021819, i.e.
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)
and
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))
How do I combine the FormatStrFormatter and useOffset syntax?
FormatStrFormatter doesn't use an offset, so by using your second format you automatically won't have an offset.
Compare the two subplots in this example
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
fig,(ax1,ax2)=plt.subplots(2)
ax1.plot(np.arange(10000,10010,1))
ax2.plot(np.arange(10000,10010,1))
ax2.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4e'))
plt.show()
Related
I have some dataset:
%matplotlib inline
import matplotlib.pyplot as plt
dataset = [28892147.7659855, 28892150.0913124, 28892148.7255983, 28892146.365328,
28892148.101613, 28892147.0887403, 28892147.8564253, 28892146.8626385,
28892146.480244, 28892146.8724146, 28892146.699191, 28892146.405013,
28892146.225238, 28892146.434353, 28892146.3250017, 28892146.344571,
28892146.494564, 28892146.36454, 28892146.8347917, 28892146.20861,
28892146.222876]
plt.plot(dataset)
by what logic matplotlib.pyplot outputs the value of 2.8892100000e7?
By default the ScalarFormatter that is used to set the tick mark labels, will work out an offset value to try and make the tick labels "round" numbers. How the offset is worked out can be found in the _compute_offset method here. How to turn off the offset is shown in the examples here. One way being, e.g.,
plt.plot(dataset)
# get axes object
ax = plt.gca()
# turn off the offset
ax.ticklabel_format(useOffset=False)
I am pretty new to Seaborn so that may sounds like a stupid question but I want to change ticks in my X-axis (which is a date) on a lineplot.
I have created a graph as follows:
import seaborn as sns
import matplotlib.pyplot as plt
g = sns.lineplot(data=to_plot.loc[ref_date:])
But I get this result:
Obviously the dates cannot be read so I would like to have one tick for every 7 days.
How to do that?
This is actually changed via Matplotlib, as Seaborn is a kind of wrapper around it.
Also, sns.lineplot return a matplotlib.axes._subplots.AxesSubplot object.
This is what you can do
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
loc = plticker.MultipleLocator(base=7.0) # To put a tick every seven days
# plt.figure(figsize=(15, 6)) # to change the size of the graph
# plt.title(f"Some super title")
# plt.ylabel("% active cases") # if you want to add a label in y-axis
axes = sns.lineplot(data=to_plot.loc[REF_DATE:])
axes.xaxis.set_major_locator(loc)
The result can look like this
I have written following code,
import numpy as np
import matplotlib.pyplot as plt
x=np.random.randint(0,10,[1,5])
y=np.random.randint(0,10,[1,5])
x.sort(),y.sort()
fig, ax=plt.subplots(figsize=(10,10))
ax.plot(x,y)
ax.set( title="random data plot", xlabel="x",ylabel="y")
I am getting a blank figure.
Same code prints chart if I manually assign below value to x and y and not use random function.
x=[1,2,3,4]
y=[11,22,33,44]
Am I missing something or doing something wrong.
x=np.random.randint(0,10,[1,5]) returns an array if you specify the shape as [1,5]. Either you would want x=np.random.randint(0,10,[1,5])[0] or x=np.random.randint(0,10,size = 5). See: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.randint.html
Matplotlib doesn't plot markers by default, only a line. As per #Can comment, matplotlib then interprets your (1, 5) array as 5 different datasets each with 1 point, so there is no line as there is no second point.
If you add a marker to your plot function then you can see the data is actually being plotted, just probably not as you wish:
import matplotlib.pyplot as plt
import numpy as np
x=np.random.randint(0,10,[1,5])
y=np.random.randint(0,10,[1,5])
x.sort(),y.sort()
fig, ax=plt.subplots(figsize=(10,10))
ax.plot(x,y, marker='.') # <<< marker for each point added here
ax.set( title="random data plot", xlabel="x",ylabel="y")
So I am looking to simply format the offset string (at least that is what i think it is called, see image) that matplotlib places along with an axis that has been set to show tick labels in scientific notation, but where the range is less than one order of magnitude (power of 10).
here is what I am talking about:
Essentially, how do I make it bigger/coloured?
you can use ax.yaxis.get_offset_text() to access the offset text. You can then set the size and color on that Text object. For example:
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots()
ax.plot(range(10),np.linspace(0,1e11,10))
offset_text = ax.yaxis.get_offset_text()
offset_text.set_size(20)
offset_text.set_color('red')
plt.show()
In matplotlib, the axes are sometimes displayed in standard form. The numbers are shown by the ticks and something like '1e-7' appears by the axis. Is there a way to change that to a written out $\times 10^{-7}$?
I am not looking to change the labels on each individual tick. I am looking to change the text that appears once at the bottom of the axis saying '1e-7'.
The simplest answer: Use the latex mode:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
x = np.arange(10000, 10011)
plt.plot(x)
plt.show()
Result:
EDIT:
Actually you don't need to use latex at all. The ScalarFormatter which is used by default has an option to use scientific notation:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
x = np.arange(10000, 10011)
fig, ax = plt.subplots(1)
ax.plot(x)
formatter = mticker.ScalarFormatter(useMathText=True)
ax.yaxis.set_major_formatter(formatter)
plt.show()
Result:
Have a look at matplotlib.ticker. It allows you control of the formatting of ticks including the labels.