Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

matplotlib force scientific notation and define exponent

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_order_of_magnitude(self):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin=None, vmax=None):
        self.format = self.fformat
        if self._useMathText:
            self.format = r'$mathdefault{%s}$' % self.format


x = np.linspace(1,9,9)
y1 = x*10**(-4)
y2 = x*10**(-3)

fig, ax = plt.subplots(2,1,sharex=True)

ax[0].plot(x,y1)
ax[1].plot(x,y2)

for axe in ax:
    axe.yaxis.set_major_formatter(OOMFormatter(-4, "%1.1f"))
    axe.ticklabel_format(axis='y', style='sci', scilimits=(-4,-4))

plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: h2o dataframe columns drop 
Python :: Plot kdeplot, lineplot, scatterplot in seaborn 
Python :: read excel by row and output to txt 
Python :: add last item of array at the first index of the array python 
Python :: sort np list of string 
Python :: fonts in python 
Python :: default dictionary value 
Python :: ord() python 
Python :: gui python 
Python :: pandas iter groups 
Python :: python get global variable by name 
Python :: transform dictionary keys python 
Python :: Range all columns of df such that the minimum value in each column is 0 and max is 1. in pandas 
Python :: scapy get packet source ip address python 
Python :: count number of subdirectories 
Python :: combinations 
Python :: base64 python 
Python :: sort decreasing python 
Python :: speak by a discord bot in python 
Python :: tensorflow use growing memory 
Python :: install pytorch on nvidia jetson nx 
Python :: Iterate through string in python using for loop 
Python :: container with most water python code leetcode 
Python :: pandas get highest values column 
Python :: list length python 
Python :: pandas groupby 
Python :: import modules given the full path python 
Python :: python histogram one liners 
Python :: :: python 
Python :: list in python 3 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =