In [3]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from dsutil.plotting import add_grid
from platform import python_version

python_version(), np.__version__,matplotlib.__version__
Out[3]:
('3.6.9', '1.19.0', '3.2.2')

Comma as thousands separator

In [64]:
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

plt.clf()

# generate sample data for this example
xs = [1,2,3,4,5,6,7,8,9,10,11,12]
ys=np.random.normal(loc=10000,size=12, scale=20000.55) + 100000.05

# plot the data
plt.bar(xs,ys)

# after plotting the data, format the labels
current_values = plt.gca().get_yticks()
plt.gca().set_yticklabels(['{:,.0f}'.format(x) for x in current_values])

add_grid()
plt.show()

Disable Scientific Notation

In [78]:
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

plt.clf()

# generate sample data for this example
xs = [1,2,3,4,5,6,7,8,9,10,11,12]
ys=np.random.normal(loc=0,size=12, scale=500000) + 1000000

# plot the data
plt.bar(xs,ys)

# after plotting the data, format the labels
current_values = plt.gca().get_yticks()
plt.gca().set_yticklabels(['{:.0f}'.format(x) for x in current_values])

add_grid()
plt.show()

Format yaxis as percentages

In [86]:
np.repeat(1.0, 12)
Out[86]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [90]:
np.minimum(np.random.normal(loc=1,size=12,scale=0.4), 
           np.repeat(1.0, 12))
Out[90]:
array([1.        , 0.21613195, 0.46872558, 1.        , 1.        ,
       1.        , 0.95374069, 0.87955852, 0.4085912 , 0.71206232,
       0.81574449, 1.        ])
In [104]:
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)

plt.clf()

# generate sample data for this example
xs = [1,2,3,4,5,6,7,8,9,10,11,12]
ys= np.minimum(np.random.normal(loc=0.5,size=12,scale=0.4), np.repeat(1.0, 12))

# plot the data
plt.bar(xs,ys)

plt.ylim(0,1.05)

# after plotting the data, format the labels
current_values = plt.gca().get_yticks()
plt.gca().set_yticklabels(['{:,.0%}'.format(x) for x in current_values])

add_grid()
plt.show()