2

I have trouble removing the scientific notation from the y axis on my matplotlib chart. (I want to display the original str or float held in the variable, If I use plt.yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) I get an attribute arror: 'matplotlib.pyplot' has no attribute 'yaxis'. Also tired plt.ticklabel_format(useOffset=False, style='plain'), that did nothing. (no error either)

from binance.client import Client
import time, os, csv, datetime
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mpl
from matplotlib.ticker import ScalarFormatter

client = Client(apikey, apisecret)

mpl.rcParams['toolbar'] = 'None'
fig = plt.figure(figsize=(4,3))
plt.style.use('ggplot')
plt.rcParams['ytick.right'] = plt.rcParams['ytick.labelright'] = True
plt.rcParams['ytick.left'] = plt.rcParams['ytick.labelleft'] = False

x_vars = []
y_vars = []

def animate(i):
    global x_vars
    global y_vars
    if len(x_vars) > 30:
        x_vars = x_vars[-30:] 
        y_vars = y_vars[-30:] 
    else:
        pass

    current_time = client.get_server_time()
    current_price = client.get_symbol_ticker(symbol="XRPBTC")

    trstime = current_time["serverTime"] / 1000
    time = datetime.datetime.fromtimestamp(int(trstime)).strftime('%M:%S')

    x_vars.append(str(time))
    y_vars.append(float(current_price["price"]))

    plt.cla()
    plt.plot(x_vars, y_vars)
    plt.xticks(rotation = 45)  


ani = animation.FuncAnimation(fig, animate, interval=500)
plt.tight_layout()
plt.show()
phpnoob
  • 141
  • 2
  • 11
  • Perhaps `plt.gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False))`? The yaxis attribute refers to the axis object, whereas plt is more equivalent to the figure object; gca stands for "get current axis". – Patol75 Jan 29 '21 at 04:34
  • Thanks, error doesn't show up anymore but I still get the scientific notation. :( – phpnoob Jan 30 '21 at 01:29

1 Answers1

3

Ok, it is working now, just had to insert plt.ticklabel_format(style='plain', useOffset=False, axis='y') inside the animate function.

phpnoob
  • 141
  • 2
  • 11