Matplotlib 应用 xaxis 和 yaxis 数字格式化
Matplotlib apply xaxis and yaxis number formatting
- 为什么我的 xaxis 没有格式化为日期?我希望它是因为我将数据帧索引设置为日期时间索引。
- 如何让所有子图共享相同的 x 轴? 我目前使用
add_subplot
而不是 plt.subplots
的原因是因为我不能让 plt.subplots 为我想做的事情工作——即制作 nrows
和 ncols
dynamic 参数,以便我可以输出任何图表我想要的形状:(4,1)形状,(2,2),(1,4)等
- 如何对每个 yaxis 应用特定的数字格式? 下面,我试图在
d
(dict)中查找绘图字符串到 return 格式字符串,然后将该格式应用于 yaxis 格式,但它似乎不起作用。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.style.use('ggplot')
df = pd.DataFrame({'Clicks': {0: 334, 1: 554, 2: 433, 3: 5353, 4: 433},
'Impressions': {0: 3242, 1: 43345, 2: 3456, 3: 34543, 4: 3453},
'Day': {0: '12/1/2015', 1: '12/2/2015', 2: '12/3/2015', 3: '12/4/2015', 4: '12/5/2015'},
'Conv': {0: 23, 1: 23, 2: 45, 3: 56, 4: 45},
'Cost': {0: 414.16, 1: 686.96, 2: 536.91, 3: 637.72, 4: 536.91}},
columns=['Day', 'Impressions', 'Clicks', 'Cost', 'Conv'])
df['Day'] = pd.to_datetime(df['Day'])
df = df.set_index('Day').resample('d', how='sum')
window = 2
nrows = 2
ncols = 2
plots = ['Impressions', 'Clicks', 'Cost', 'Conv']
d = {'Impressions':'{:,.0f}', 'Clicks': '{:,.0f}', 'Cost':'${:,.2f}', 'Conv': '{:,.0f}'}
fig = plt.figure(figsize=(8,6))
for i, plot in enumerate(plots):
ax = fig.add_subplot(nrows, ncols, i+1)
ax.plot(df.index, df[plot])
ma = pd.rolling_mean(df[plot], window)
ax.plot(df.index, ma)
mstd = pd.rolling_std(df[plot], window)
ax.fill_between(df.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.1)
ax.set_title(plot)
ax.get_yaxis().set_major_formatter(FuncFormatter(lambda x, p: d[plot].format(x)))
plt.tight_layout()
plt.show()
这是df
:
Impressions Clicks Cost Conv
Day
2015-12-01 3242 334 414.16 23
2015-12-02 43345 554 686.96 23
2015-12-03 3456 433 536.91 45
2015-12-04 34543 5353 637.72 56
2015-12-05 3453 433 536.91 45
Why is my xaxis not formatted as a date?
您需要将 DateFormatter
(或类似的)设置为 major_formatter
- 请参阅下面的代码。
How do I make all subplots share the same xaxis?
向子图调用添加 sharex=True
参数。
如果像下面的代码所示那样将它们展平,则可以使用 .subplots()
中的轴。
How do I apply a specific number format to each yaxis?
您的 FuncFormatter
需要 return 来自给定 tick_value
和 position
的格式化字符串,如下面的代码所示:
fig, axes = plt.subplots(2, 2, figsize=(8,6), sharex=True)
for ax, plot in zip(axes.flat, plots):
ax.plot(df.index, df[plot])
ma = pd.rolling_mean(df[plot], window)
ax.plot(df.index, ma)
mstd = pd.rolling_std(df[plot], window)
ax.fill_between(df.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.1)
ax.set_title(plot)
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, p: '{:.0f}'.format(x)))
ax.xaxis.set_major_formatter(DateFormatter('%d-%H:%M')) # or '%d.%m.%y'
fig.autofmt_xdate() # This will rotate the xticklabels by 30 degrees so that all dates are readable.
fig.tight_layout() # no need to call this inside the loop.
这将产生如下图:
- 为什么我的 xaxis 没有格式化为日期?我希望它是因为我将数据帧索引设置为日期时间索引。
- 如何让所有子图共享相同的 x 轴? 我目前使用
add_subplot
而不是plt.subplots
的原因是因为我不能让 plt.subplots 为我想做的事情工作——即制作nrows
和ncols
dynamic 参数,以便我可以输出任何图表我想要的形状:(4,1)形状,(2,2),(1,4)等 - 如何对每个 yaxis 应用特定的数字格式? 下面,我试图在
d
(dict)中查找绘图字符串到 return 格式字符串,然后将该格式应用于 yaxis 格式,但它似乎不起作用。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.style.use('ggplot')
df = pd.DataFrame({'Clicks': {0: 334, 1: 554, 2: 433, 3: 5353, 4: 433},
'Impressions': {0: 3242, 1: 43345, 2: 3456, 3: 34543, 4: 3453},
'Day': {0: '12/1/2015', 1: '12/2/2015', 2: '12/3/2015', 3: '12/4/2015', 4: '12/5/2015'},
'Conv': {0: 23, 1: 23, 2: 45, 3: 56, 4: 45},
'Cost': {0: 414.16, 1: 686.96, 2: 536.91, 3: 637.72, 4: 536.91}},
columns=['Day', 'Impressions', 'Clicks', 'Cost', 'Conv'])
df['Day'] = pd.to_datetime(df['Day'])
df = df.set_index('Day').resample('d', how='sum')
window = 2
nrows = 2
ncols = 2
plots = ['Impressions', 'Clicks', 'Cost', 'Conv']
d = {'Impressions':'{:,.0f}', 'Clicks': '{:,.0f}', 'Cost':'${:,.2f}', 'Conv': '{:,.0f}'}
fig = plt.figure(figsize=(8,6))
for i, plot in enumerate(plots):
ax = fig.add_subplot(nrows, ncols, i+1)
ax.plot(df.index, df[plot])
ma = pd.rolling_mean(df[plot], window)
ax.plot(df.index, ma)
mstd = pd.rolling_std(df[plot], window)
ax.fill_between(df.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.1)
ax.set_title(plot)
ax.get_yaxis().set_major_formatter(FuncFormatter(lambda x, p: d[plot].format(x)))
plt.tight_layout()
plt.show()
这是df
:
Impressions Clicks Cost Conv
Day
2015-12-01 3242 334 414.16 23
2015-12-02 43345 554 686.96 23
2015-12-03 3456 433 536.91 45
2015-12-04 34543 5353 637.72 56
2015-12-05 3453 433 536.91 45
Why is my xaxis not formatted as a date?
您需要将 DateFormatter
(或类似的)设置为 major_formatter
- 请参阅下面的代码。
How do I make all subplots share the same xaxis?
向子图调用添加 sharex=True
参数。
如果像下面的代码所示那样将它们展平,则可以使用 .subplots()
中的轴。
How do I apply a specific number format to each yaxis?
您的 FuncFormatter
需要 return 来自给定 tick_value
和 position
的格式化字符串,如下面的代码所示:
fig, axes = plt.subplots(2, 2, figsize=(8,6), sharex=True)
for ax, plot in zip(axes.flat, plots):
ax.plot(df.index, df[plot])
ma = pd.rolling_mean(df[plot], window)
ax.plot(df.index, ma)
mstd = pd.rolling_std(df[plot], window)
ax.fill_between(df.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.1)
ax.set_title(plot)
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, p: '{:.0f}'.format(x)))
ax.xaxis.set_major_formatter(DateFormatter('%d-%H:%M')) # or '%d.%m.%y'
fig.autofmt_xdate() # This will rotate the xticklabels by 30 degrees so that all dates are readable.
fig.tight_layout() # no need to call this inside the loop.
这将产生如下图: