How to remove scientific notation on the y-axis using matplotlib?

Asked

Viewed 142 times

3

When generating a graph using the library matplotlib in Python language in the Jupyter Notebook, I realized that the data presented in it were "reduced" (I think that would be the term) and with a strange nomenclature on top of the Y axis (le7). The data generated were between 100,000 and 1,000,000 to perform study tests on the library, since I am a beginner. Below follows the generated graph:

Gráfico gerado

Could I make the "reduced numbers" stick to their "normal" values? What would this nomenclature be le7? I’m using the library sqlite3, extracting information from a database from a single file. Below follows the code that was done to generate the graph:

import matplotlib.pyplot as plt
from sqlite3 import *

conn = connect('C:\\Users\\lucas\\Documents\\Bancos\\empresas.db')
c = conn.cursor()

a = c.execute("SELECT empresa, SUM(acoes) FROM empresas GROUP BY empresa;")

empresa = []
acao = []

for i in a:
    empresa.append(i[0])
    acao.append(i[1])
    
plt.xlabel('Empresas')
plt.ylabel('Ações')

plt.bar(empresa,acao)
plt.show()

To know about the values, the data that were returned in the database were these:

inserir a descrição da imagem aqui

1 answer

2


1e7 is indicating that the scale is in tens of millions (10 7). In general, it is recommended to move the scale to millions (10 6) or thousands (10 3) to facilitate the reading of the graph. But if you want to keep in the original form just use the method plt.ticklabel_format:

plt.xlabel('Empresas')
plt.ylabel('Ações')
plt.bar(empresa,acao)
plt.ticklabel_format(style='plain', axis='y')
plt.show()

inserir a descrição da imagem aqui

  • Thank you very much! Could you also tell me how to put the values on top of each bar of the bar graph? Grateful!

  • You’ll need to use plt.annotate. See if this answer answers your question: https://answall.com/questions/360662/mostrar-valor-dentro-de-uma-barra-com-matplotlib If not, open a new question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.