I want to format my y axis using matplotlib in python 2.7. This is what I tried:
ax.yaxis.get_major_formatter().set_useLocale()
to format my y axis using . as thousands separator. Instead of having 10000, I'd like to have 10.000, and so on... but I can't find any example on how this work...
I could not find the documentation, on this page here there is no example or further documentation: http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.ScalarFormatter.set_useLocale
Or any other idea on how to format my axis?
thanks
I believe that you are looking for more control than perhaps set_useLocale() can offer. Therefore, drawing upon the example given here, I've used FuncFormatter with a simple function. The comma_format function inserts the y-axis labels with a comma as a thousands separator and then replaces the commas with periods. In this way, the y-axis labels can be formatted rather easily.
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
def comma_format(x, p):
return format(x, "6,.0f").replace(",", ".")
ax = subplot(111)
xx = np.arange(0,20,1)
yy = np.arange(1000,10000,450)
ax.get_yaxis().set_major_formatter(ticker.FuncFormatter(comma_format))
plt.scatter(xx,yy)
plt.show()
Related
I am trying to create a chart using a data frame which has TimePeriod as 201811, 201812, 201901, ..., 202006 which I want to use as the x axis values and plot against the y values (Total lives). See figure here:
However, when I plot the figure the x axis shows up as 201825, 201850, 201875, 201925,..., 202025. This clearly makes no sense and I cannot figure out how to force python to plot the desired x axis.
I am assuming it is something in xticks but I haven't has any luck. I have also tried manually entering all x axis values as labels = ('201811', '201812', '201901', ...) but this did not work either.
Is there any way to achieve the desired outcome?
Code:
import numpy as np
import pyodbc
import matplotlib.pyplot as plt
aggregated_lives_plt = aggregated_lives.plot(x= 'TimePeriodId', y='TotalLives', kind = 'line')
plt.title('Aggregated Optional Benefit Certs Since Nov-2018')
plt.xlabel('Time Period')
plt.ylabel('Total Certs (Lives)')
plt.show()
Thank you for any help!
You Timeperiod is integers, you can convert it to string:
aggregated_lives['TimePeriodId'] = aggregated_lives['TimePeriodId'].astype(str)
then use your plot command.
I work on a plot in python using the matplot library. The numbers which I have to generate are very big, so also the ticks on the axes are a large numbers and take a lot of space. I was trying to present them as a powers (for example instead having a tick 100000000 I want to have 10^8). I used command: ax.ticklabel_format(style='sci', axis='x', scilimits=(0,4)) however this only created something like this
Is there any other solution to have ticks for the plot as: 1 x 10^4, 2 x 10^4, etc or write the value 1e4 as 10^4 at the end of the label's ticks?
You can use the matplotlib.ticker module, and set the ax.xaxis.set_major_formatter to a FuncFormatter.
For example:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
plt.rcParams['text.usetex'] = True
fig,ax = plt.subplots(1)
x = y = np.arange(0,1.1e4,1e3)
ax.plot(x,y)
def myticks(x,pos):
if x == 0: return "$0$"
exponent = int(np.log10(x))
coeff = x/10**exponent
return r"${:2.0f} \times 10^{{ {:2d} }}$".format(coeff,exponent)
ax.xaxis.set_major_formatter(ticker.FuncFormatter(myticks))
plt.show()
Note, this uses LaTeX formatting (text.usetex = True) to render exponents in the tick labels. Also note the double braces required to differentiate the LaTeX braces from the python format string braces.
There might be a better solution, but if you know the values of each xtick, you can also manually name them.
Here is an example:
http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html
I am trying to plot f in this program but I am screwing something up. Can someone have a look and inform me as to where I am messing up. Thanks.
import math
#x is the horizontal distance that the ball has traveled
g=9.81
v=raw_input('Enter an initial velocity:')
theta=raw_input('Enter the angle that the object was thrown at:')
y=raw_input('Enter the initial position of the object on the y-axis:')
t=(2*v*math.sin(theta))/g
x=(0.5)*((v*math.sin(theta))+v)*t
float(v)
float(theta)
float(y)
float(t)
f=x*math.tan(theta)-(1/(2*(v**2)))*((g(x**2))/(math.cos(theta)**2))+y
figure(1)
clf()
plot(f)
xlabel('x')
ylabel('y')
show()
So first of all, I would import numpy and matplotlib
import numpy as np
import matplotlib.pyplot as plt
Then, you have to convert your string input into floats, for that you can use eval.
initial_velo = eval(raw_input("Whatever you like: "))
...
Then for plotting with matplotlib you actually have to create a list of values (just as when you collect real data and then type it into the computer and then plot the single data points). For that I like to use linspace from the numpy import:
time_steps = np.linspace(0, t, steps)
# steps gives the numbers of intervals your time from 0 to t is splitted into
Now you create your functions x and f as functions of t. They will also have to be of type list. And in the end you can plot what you want via:
plt.figure(1)
plt.plot(time_steps, f)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
But maybe you should also watch how to plot stuff in the matplotlib doc. Also numpy has a great doc.
I have a line chart based on a simple list of numbers. By default the x-axis is just the an increment of 1 for each value plotted. I would like to be a percentage instead but can't figure out how. So instead of having an x-axis from 0 to 5, it would go from 0% to 100% (but keeping reasonably spaced tick marks. Code below. Thanks!
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid.axislines import Subplot
data=[8,12,15,17,18,18.5]
fig=plt.figure(1,(7,4))
ax=Subplot(fig,111)
fig.add_subplot(ax)
plt.plot(data)
The code below will give you a simplified x-axis which is percentage based, it assumes that each of your values are spaces equally between 0% and 100%.
It creates a perc array which holds evenly-spaced percentages that can be used to plot with. It then adjusts the formatting for the x-axis so it includes a percentage sign using matplotlib.ticker.FormatStrFormatter. Unfortunately this uses the old-style string formatting, as opposed to the new style, the old style docs can be found here.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mtick
data = [8,12,15,17,18,18.5]
perc = np.linspace(0,100,len(data))
fig = plt.figure(1, (7,4))
ax = fig.add_subplot(1,1,1)
ax.plot(perc, data)
fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
xticks = mtick.FormatStrFormatter(fmt)
ax.xaxis.set_major_formatter(xticks)
plt.show()
This is a few months late, but I have created PR#6251 with matplotlib to add a new PercentFormatter class. With this class you can do as follows to set the axis:
import matplotlib.ticker as mtick
# Actual plotting code omitted
ax.xaxis.set_major_formatter(mtick.PercentFormatter(5.0))
This will display values from 0 to 5 on a scale of 0% to 100%. The formatter is similar in concept to what #Ffisegydd suggests doing except that it can take any arbitrary existing ticks into account.
PercentFormatter() accepts three arguments, max, decimals, and symbol. max allows you to set the value that corresponds to 100% on the axis (in your example, 5).
The other two parameters allow you to set the number of digits after the decimal point and the symbol. They default to None and '%', respectively. decimals=None will automatically set the number of decimal points based on how much of the axes you are showing.
Note that this formatter will use whatever ticks would normally be generated if you just plotted your data. It does not modify anything besides the strings that are output to the tick marks.
Update
PercentFormatter was accepted into Matplotlib in version 2.1.0.
Totally late in the day, but I wrote this and thought it could be of use:
def transformColToPercents(x, rnd, navalue):
# Returns a pandas series that can be put in a new dataframe column, where all values are scaled from 0-100%
# rnd = round(x)
# navalue = Nan== this
hv = x.max(axis=0)
lv = x.min(axis=0)
pp = pd.Series(((x-lv)*100)/(hv-lv)).round(rnd)
return pp.fillna(navalue)
df['new column'] = transformColToPercents(df['a'], 2, 0)
I'm very new to using matplotlib, and I'm having difficulty with the xticks. I basically have an x axis from 0 to 0.025. My problem arises since the precision of the most precise value in the x axis seems to set the precision for them all, so e.g. 0 appears as 0.000. I'd like it to appear as 0 since the trailing zeroes are redundant and analogously for the other values.
Here is what I have... the output gives too many trailing zeroes on the x axis:
from matplotlib import rc
from matplotlib import pyplot
import matplotlib.pyplot as plt
rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex = True)
xmin=0
xmax=0.4
ymin=4.0
ymax=4.5
asq=[0.0217268]
mb=[4.1929]
mberr=[0.0055]
# some arguments for points etc...
ebargs = dict(mfc='None',alpha=1,ms=8,
capsize=1.75,elinewidth=0.75,mew=0.75)
fw = 4.5 # width
fh = fw/1.618 # height
plt.rc('figure',figsize=(fw,fh))
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)
plt.errorbar(x=[x for x in asq],
y=[y for y in mb],
yerr=[yerr for yerr in mberr],
fmt='o',c='b',mec='b', **ebargs
)
plt.savefig("mb-plot.pdf",bbox_inches='tight')
Is there an obvious way to do what I'd like, or am I stuck with it? I used PyX previously (and I must admit I'm getting a bit muddled as I've learned to use each purely through the use of stuff my collaborators have used and they've varied between), which sets the axes properly, but doesn't seem to support LaTeX as well as I'd like, so it's not an idea solution.
What you need are these two lines:
from matplotlib.ticker import FormatStrFormatter
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%g'))
The FormatStrFormatter can accept other sprintf-like formatting options.