使matplotlib图默认情况下看起来像R? [英] making matplotlib graphs look like R by default?
问题描述
在绘制默认值方面,是否有办法使matplotlib
的行为与R相同,或几乎与R相似?例如,R将其轴与matplotlib
完全不同.以下直方图
Is there a way to make matplotlib
behave identically to R, or almost like R, in terms of plotting defaults? For example R treats its axes pretty differently from matplotlib
. The following histogram
具有带有向外刻度的浮动轴",这样就没有内部刻度(与matplotlib
不同),并且这些轴不会越过原点近".而且,直方图可以溢出"到未用刻度标记的值,例如x轴以3结束,但直方图略微超出它.对于matplotlib
中的所有直方图如何自动实现?
has "floating axes" with outward ticks, such that there are no inner ticks (unlike matplotlib
) and the axes do not cross "near" the origin. Also, the histogram can "spillover" to values that are not marked by the tick - e.g. the x-axis ends at 3 but the histograms extends slightly beyond it. How can this be achieved automatically for all histograms in matplotlib
?
相关问题:散点图和折线图在R中具有不同的默认轴设置,例如:
Related question: scatter plots and line plots have different default axes settings in R, for example:
没有内部刻度线,并且刻度线朝外.同样,刻度线在原点之后稍稍开始(y和x轴在轴的左下角交叉),刻度线在轴终点之前稍稍结束.这样,最低的x轴刻度线和最低的y轴刻度线的标签就无法真正交叉,因为它们之间有一定的空间,这使绘图具有非常优雅的整洁外观.请注意,轴刻度标签和刻度本身之间也有相当大的空间.
There no inner ticks again and the ticks face outward. Also, the ticks start slightly after the origin point (where the y and x axes cross at the bottom left of the axes) and the ticks end slightly before the axes end. This way the labels of the lowest x-axis tick and lowest y-axis tick can't really cross, because there's a space between them and this gives the plots a very elegant clean look. Note that there's also considerably more space between the axes ticklabels and the ticks themselves.
此外,默认情况下,未标记的x或y轴上没有刻度线,这意味着左侧的y轴与右侧的标记的y轴平行没有刻度线,并且x相同轴,再次消除了图中的混乱情况.
Also, by default there are no ticks on the non-labeled x or y axes, meaning the y-axis on the left that is parallel to the labeled y-axis on the right has no ticks, and same for the x-axis, again removing clutter from the plots.
有没有办法使matplotlib看起来像这样?通常情况下,默认情况下看起来与默认R图一样多吗?我非常喜欢matplotlib
,但是我认为R默认值/开箱即用的绘图行为确实可以解决问题,并且其默认设置很少会导致刻度标签重叠,数据混乱或混乱,因此我希望使用默认值尽可能地像这样.
Is there a way to make matplotlib look like this? And in general to look by default as much as default R plots? I like matplotlib
a lot but I think the R defaults / out-of-the-box plotting behavior really have gotten things right and its default settings rarely lead to overlapping tick labels, clutter or squished data, so I would like the defaults to be as much like that as possible.
推荐答案
一年后
使用seaborn
,下面的示例变为:
Edit 1 year later:
With seaborn
, the example below becomes:
import numpy as np
import matplotlib.pyplot as plt
import seaborn
seaborn.set(style='ticks')
# Data to be represented
X = np.random.randn(256)
# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)
heights, positions, patches = axes.hist(X, color='white')
seaborn.despine(ax=axes, offset=10, trim=True)
fig.tight_layout()
plt.show()
当当很简单.
这篇博客文章是迄今为止我所见过的最好的文章. http://messymind.net/making-matplotlib-look-like-ggplot/
This blog post is the best I've seen so far. http://messymind.net/making-matplotlib-look-like-ggplot/
它不像您在大多数入门"类型示例中看到的那样专注于您的标准R图.相反,它尝试模仿ggplot2的样式,该样式似乎被普遍认为是时尚且设计精美的.
It doesn't focus on your standard R plots like you see in most of the "getting started"-type examples. Instead it tries to emulate the style of ggplot2, which seems to be nearly universally heralded as stylish and well-designed.
要像看到条形图中那样获得轴刺,请尝试遵循此处的前几个示例之一: http://www.loria.fr/~rougier/coding/gallery/
To get the axis spines like you see the in bar plot, try to follow one of the first few examples here: http://www.loria.fr/~rougier/coding/gallery/
最后,要使轴刻度线朝外指向,您可以编辑matplotlibrc
文件以说出xtick.direction : out
和ytick.direction : out
.
Lastly, to get the axis tick marks pointing outward, you can edit your matplotlibrc
files to say xtick.direction : out
and ytick.direction : out
.
将这些概念结合在一起,我们得到的东西是这样的:
Combining these concepts together we get something like this:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# Data to be represented
X = np.random.randn(256)
# Actual plotting
fig = plt.figure(figsize=(8,6), dpi=72, facecolor="white")
axes = plt.subplot(111)
heights, positions, patches = axes.hist(X, color='white')
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes.xaxis.set_ticks_position('bottom')
# was: axes.spines['bottom'].set_position(('data',1.1*X.min()))
axes.spines['bottom'].set_position(('axes', -0.05))
axes.yaxis.set_ticks_position('left')
axes.spines['left'].set_position(('axes', -0.05))
axes.set_xlim([np.floor(positions.min()), np.ceil(positions.max())])
axes.set_ylim([0,70])
axes.xaxis.grid(False)
axes.yaxis.grid(False)
fig.tight_layout()
plt.show()
可以用多种方法指定棘突的位置.如果您在IPython中运行以上代码,则可以执行axes.spines['bottom'].set_position?
来查看所有选项.
The position of the spines can be specified a number of ways. If you run the code above in IPython, you can then do axes.spines['bottom'].set_position?
to see all of your options.
是的.这并不简单,但是您可以接近.
So yeah. It's not exactly trivial, but you can get close.
这篇关于使matplotlib图默认情况下看起来像R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!