如何使用matplotlib更改单位以百万而不是1e7显示x轴

w8f9ii69  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(460)

import pyodbc
import pandas as pd
import matplotlib.pyplot as plt

conn = pyodbc.connect('Driver={SQL Server};' # This is what server type we are connecting to
                      'Server=DESKTOP-3JKKF7H;' # This is the location and name of the server, same as what we use to connect using SSMS
                      'Database=AdventureWorks2019;' # This is which database we are connecting to within the selected server
                      'Trusted_Connection=yes;') # This allows us to forgo entering a trusted key or password because we are the admin of this computer and the...
                    # ... database has been configured to allow this user when we set it up. 

cursor = conn.cursor()

query = 'SELECT * FROM Regional'

regional = pd.read_sql(query, conn)

print(regional.head())

regional.plot.barh(x='Region', y='Sales')
plt.xlabel("Sales (10s of millions)")

plt.show()
ljo96ir5

ljo96ir51#

import numpy as np
import matplotlib.pyplot as plt

data = np.array([28E6, 17E6, 7E6, 6E6, 4E6])
labels = 'Southwest Northwest Central Southeast Northeast'.split()

fig, (ax0, ax1)  = plt.subplots(ncols=2, figsize=(10, 4))
fig.set_layout_engine('constrained')

ax0.barh(labels, data)
ax0.set_xlabel('Sales')

ax1.barh(labels, data/1E6)
ax1.set_xlabel('Sales, in Millions')

plt.show()

相关问题