本文介绍了如何使matplotlib图看起来像这样专业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认的matplotlib图看起来确实没有吸引力,甚至不专业。我尝试了几个软件包,包括seaborn以及prettyplotlib,但是这两个包几乎都没有改善样式。

Default matplotlib graphs look really unattractive and even unprofessional. I tried out couple of packages include seaborn as well as prettyplotlib but both of these just barely improves the styles.

到目前为止,我已经开始使用seaborn软件包了:

So far I've gotten to following using seaborn package:

下面是我正在寻找的外观,可惜从上面:

Below is the appearance I'm looking for which is far cry from above:

在第二个示例中注意以下几点:

Notice the following niceness in the 2nd example:


  1. 已填充图形下的区域

  2. 图形线是思想者,很好地突出了。

  3. 轴线是思想者,又很好地突出了。 / li>
  4. 曲线下的区域是透明的。

  5. X轴刻度线更密集。

  1. Area under the graph is filled with much more eye pleasing color.
  2. The graph line is thinker and nicely stands out.
  3. Axis lines are thinker and again nicely stands out.
  4. Area under the curve is transparent.
  5. X-Axis tick marks are more denser.

我的问题是:您是否认为上述是我可以在matplotlib中快速使用的流行主题或样式?还是可以从某些包装中使用?如果失败了,是否有办法将此样式设置为我的全局首选项?失败了,甚至有可能在matlibplot中执行此操作?

My questions are: Do you recognize above as some kind of popular theme or style that I can quickly use in matplotlib? Or if I can use from some package? Failing that, is there anyway to set this style as my global preference? Failing that, is it even possible to do this in matlibplot?

谢谢!

推荐答案

这实际上是一个品味问题,也是一个目标受众的问题。 matplotlib 试图为科学目的制作清晰的插图。这-必定是一种折衷方案,插图不是您要在杂志上印刷或在广告中显示的东西。

This is really a matter of taste, and also a matter of target audience. matplotlib tries to produce clear illustrations for scientific purposes. This is - necessarily - a compromise, and the illustrations are not something you would print in a magazine or show in an advertisement.

有好消息,有坏消息关于 matplotlib

There are some good news and some bad news about matplotlib in this sense.

坏消息:


  • 没有一个神奇的命令或程序包可以用 matplotlib 创建漂亮的地块。

  • There is no single magical command or package which would create beautiful plots with matplotlib.

好消息:


  • 有简单的方法可以更改默认设置,请参见:

  • 对象模型使用户可以更改几乎所有内容并引入复杂的新功能。

  • 源代码可用,甚至用户也可以很容易地对其进行更改。

在我看来,最困难的事情是决定您要做什么想。然后,即使开始时学习曲线很陡,做您想做的事情也会更容易。

In my opinion the most difficult thing is to decide what you want. Then doing what you want is easier, even though there is a steepish learning curve in the beginning.

仅作为示例:

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轴限制未考虑数据的周期性。)

(Just a comment: The X-axis limits in the original image do not take the cyclicity of the data into account.)

这会给我们这样的东西:

This will give us something like this:

很容易理解,我们需要进行很多更改才能将其显示得更少注重工程的观众。至少:

It is easy to understand that we need to do a lot of changes in order to be able to show this to a less-engineering-minded audience. At least:


  • 使填充透明且颜色不那么令人反感

  • 使线变粗

  • 更改线条颜色

  • 向X轴添加更多刻度线

  • 更改标题的字体

  • make the fill transparent and less offensive in colour
  • make the line thicker
  • change the line colour
  • add more ticks to the X axis
  • change the fonts of the titles
# 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 的默认设置。也许这给出了有关如何更改情节中事物的想法。

This is not exactly as in the question, but everything can be tuned towards that direction. Many of the things set here can be set as defaults for matplotlib. Maybe this gives an idea of how to change things in the plots.

这篇关于如何使matplotlib图看起来像这样专业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:07