这真的是品味问题,也是目标受众的问题。 matplotlib 试图为科学目的制作清晰的插图。这是 - 必然 - 一种妥协,插图不是你会在杂志上打印或在广告中展示的东西。
在这个意义上,matplotlib 有好消息也有坏消息。
坏消息:
- 没有一个神奇的命令或软件包可以用
matplotlib 创造美丽的情节。
好消息:
在我看来,最困难的事情是决定你想要什么。然后做你想做的事情会更容易,即使一开始的学习曲线很陡峭。
举个例子:
import numpy as np
import matplotlib.pyplot as plt
# create some fictive access data by hour
xdata = np.arange(25)
ydata = np.random.randint(10, 20, 25)
ydata[24] = ydata[0]
# let us make a simple graph
fig = plt.figure(figsize=[7,5])
ax = plt.subplot(111)
l = ax.fill_between(xdata, ydata)
# set the basic properties
ax.set_xlabel('Time of posting (US EST)')
ax.set_ylabel('Percentage of Frontpaged Submissions')
ax.set_title('Likelihood of Reaching the Frontpage')
# set the limits
ax.set_xlim(0, 24)
ax.set_ylim(6, 24)
# set the grid on
ax.grid('on')
(注解:原图X轴限制没有考虑数据的循环性。)
这会给我们这样的东西:
很容易理解,我们需要进行大量更改才能向缺乏工程意识的观众展示这一点。至少:
- 使填充透明且颜色不那么刺眼
- 让线条变粗
- 更改线条颜色
- 向 X 轴添加更多刻度
- 更改标题的字体
# change the fill into a blueish color with opacity .3
l.set_facecolors([[.5,.5,.8,.3]])
# change the edge color (bluish and transparentish) and thickness
l.set_edgecolors([[0, 0, .5, .3]])
l.set_linewidths([3])
# add more ticks
ax.set_xticks(np.arange(25))
# remove tick marks
ax.xaxis.set_tick_params(size=0)
ax.yaxis.set_tick_params(size=0)
# change the color of the top and right spines to opaque gray
ax.spines['right'].set_color((.8,.8,.8))
ax.spines['top'].set_color((.8,.8,.8))
# tweak the axis labels
xlab = ax.xaxis.get_label()
ylab = ax.yaxis.get_label()
xlab.set_style('italic')
xlab.set_size(10)
ylab.set_style('italic')
ylab.set_size(10)
# tweak the title
ttl = ax.title
ttl.set_weight('bold')
现在我们有了:
这与问题中的不完全一样,但一切都可以朝着这个方向调整。此处设置的许多内容都可以设置为matplotlib 的默认值。也许这给出了如何改变情节的想法。