用数千个分隔符和字体大小格式化y轴matplotlib [英] Formatting y-axis matplotlib with thousands separator and font size

查看:37
本文介绍了用数千个分隔符和字体大小格式化y轴matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码有效.

名称 = 1960 - 2016 年

值=每年美国的GDP

代码生成两个图表,但y轴标签从5000到175000运行.我想1) 用,"格式化标签,作为千位分隔符,例如,5,000 或 17,500,以及,

2)我想增加标签的字体大小-因此,请增加例如5000的字体.

在线找不到可行/可理解的示例.帮助表示赞赏.

%matplotlib 内联导入matplotlib.pyplot作为pltplt.style.use('seaborn-white')将numpy导入为np从numpy导入数组# 绘制 GDP/年名称 = usa.loc[: , "Year"]values = usa.loc[: , "GDP 十亿"]plt.figure(1,figsize =(15,6))plt.suptitle('GDP 增长', fontsize=20)plt.subplot(121)plt.plot(名称,值)plt.xticks(np.arange(0,57,step = 5.0))plt.ylabel('GDP',fontsize = 16)plt.title('United States',fontsize = 16)plt.subplot(122)plt.plot(名称,值)plt.xticks(np.arange(0, 57, step=5.0))plt.xlabel('Year', fontsize=16)plt.title('美国',fontsize=16)#plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4))#print(plt.xticks())plt.show()

解决方案

1)用,"表示标签的格式,以千位分隔符的形式表示,例如5,000或17,500,以及(如

The code below works.

names = years 1960 - 2016

values = GDP of the US for each year

The code produces two charts, but the y-axis label runs from 5000 to 175000. I want to 1) format the label with a "," for a thousands separator, so as an example, 5,000 or 17,500, and,

2) I want to increase the font size of the labels - so increase the font of, for example, 5000.

Not finding a workable/understandable example on line. Help appreciated.

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
from numpy import array

# Plot GDP/Year

names =  usa.loc[: , "Year"]
values = usa.loc[: , "GDP Billions"]

plt.figure(1, figsize=(15, 6))
plt.suptitle('GDP Growth', fontsize=20)

plt.subplot(121)
plt.plot(names, values)
plt.xticks(np.arange(0, 57, step=5.0))
plt.ylabel('GDP', fontsize=16)
plt.title('United States',fontsize=16)

plt.subplot(122)
plt.plot(names, values)
plt.xticks(np.arange(0, 57, step=5.0))
plt.xlabel('Year', fontsize=16)
plt.title('United States',fontsize=16)

#plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4))
#print(plt.xticks())


plt.show()

解决方案

1) format the label with a "," for a thousands separator, so as an example, 5,000 or 17,500, and (as in How do I format axis number format to thousands with a comma in matplotlib?)

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

2) I want to increase the font size of the labels - so increase the font of, for example, 5000:

plt.rc('ytick', labelsize=5000) 

Here's how you change your code to incorporate these solutions (as requested in the comment):

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
from numpy import array

plt.rc('ytick', labelsize=12) 

# Plot GDP/Year
names =  usa.loc[: , "Year"]
values = usa.loc[: , "GDP Billions"]


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))

fig.suptitle('GDP Growth', fontsize=20)

ax1.plot(names, values)
ax1.set_xticklabels(np.arange(0, 57, step=5.0))
ax1.set_ylabel('GDP', fontsize=16)
ax1.set_title('United States',fontsize=16)
ax1.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

ax2.plot(names, values)
ax2.set_xticklabels(np.arange(0, 57, step=5.0))
ax2.set_ylabel('Year', fontsize=16)
ax2.set_title('United States',fontsize=16)
ax2.get_yaxis().set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))
#plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 4))
#print(plt.xticks())


plt.show()

And here's what the plots look like for a dummy data I created:

这篇关于用数千个分隔符和字体大小格式化y轴matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆