Python Y标签轴值1e6更改
我已经从pandas dataframe为我的实验室课程创建了一个活跃的新冠病毒病例条形图。虽然我对y轴和它的参考(1E6)不满意,但我如何更改它 我的DF 我用这种方法画了这个图Python Y标签轴值1e6更改,python,pandas,Python,Pandas,我已经从pandas dataframe为我的实验室课程创建了一个活跃的新冠病毒病例条形图。虽然我对y轴和它的参考(1E6)不满意,但我如何更改它 我的DF 我用这种方法画了这个图 plt.style.use('ggplot') ax = newdf[['confirmed','deaths']].plot(kind='bar', title ="Covid most active cases countries",figsize=(15,10),legend=True,
plt.style.use('ggplot')
ax = newdf[['confirmed','deaths']].plot(kind='bar', title ="Covid most active cases countries",figsize=(15,10),legend=True, fontsize=12,color=['blue','green'])
ax.set_xlabel("Countires",fontsize=12)
ax.set_ylabel("Cases",fontsize=12)
plt.show()
结果是:
您可以格式化y轴
- 第一个图形是默认的格式化程序,它给出和您注意到的相同的结果
- 第二个图形格式为百万
import matplotlib.pyplot as plt
import matplotlib
fig, ax = plt.subplots(2, figsize=[14,6], sharex=True)
df = pd.json_normalize(requests.get("https://corona-api.com/countries").json()["data"])
dfp = (df.loc[:,["name","latest_data.deaths","latest_data.confirmed"]]
.set_index("name")
.sort_values("latest_data.deaths",ascending=False)
.head(20)
)
dfp.plot(ax=ax[0], kind="bar")
dfp.plot(ax=ax[1], kind="bar")
ax[1].get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x)//10**6, ',')))