58 python plot r matplotlib pandas
matplotlib在绘制默认值方面,有没有办法使R的行为与R相同,或者几乎与R相同?例如,R对待它的轴与它完全不同matplotlib.以下直方图

具有向外刻度的"浮动轴",使得没有内部刻度(不同matplotlib)并且轴不会"接近"原点.此外,直方图可以"溢出"到未通过刻度标记的值 - 例如,x轴以3结束,但直方图略微超出它.如何在所有直方图中自动实现matplotlib?
相关问题:散点图和线图在R中具有不同的默认轴设置,例如:

再没有内部蜱,蜱面朝外.此外,在原点(y轴和x轴在轴的左下方交叉)之后,刻度开始略微,并且在轴结束之前刻度稍微结束.这样,最低x轴刻度和最低y轴刻度的标签不能真正交叉,因为它们之间有一个空间,这使得绘图非常优雅干净.请注意,轴刻度标签和刻度本身之间的空间也相当大.
此外,默认情况下,未标记的x或y轴上没有刻度,这意味着左侧的y轴与右侧标记的y轴平行,没有刻度,x轴相同,再次消除阴谋的混乱.
有没有办法让matplotlib看起来像这样?一般来说,默认情况下看默认R图是多少?我喜欢matplotlib很多,但我认为R默认/开箱即用的绘图行为确实让事情变得正确,其默认设置很少会导致重叠的刻度标签,杂乱或压扁的数据,所以我希望默认设置为尽可能地这样.
Pau*_*l H 44
使用seaborn,以下示例变为:
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()
Run Code Online (Sandbox Code Playgroud)
相当容易.
这篇博文是迄今为止我见过的最好的. http://messymind.net/making-matplotlib-look-like-ggplot/
它并没有像你在大多数"入门"类型的例子中看到的那样关注你的标准R图.相反,它试图模仿ggplot2的风格,这似乎几乎普遍被称为时尚和精心设计.
要获得轴刺,就像你看到条形图一样,请尝试按照前面几个例子之一:http://www.loria.fr/~rougier/coding/gallery/
最后,要使轴刻度标记指向外部,您可以编辑matplotlibrc文件以说明xtick.direction : out和ytick.direction : out.
将这些概念结合在一起我们得到这样的结论:
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()
Run Code Online (Sandbox Code Playgroud)
可以通过多种方式指定脊柱的位置.如果您在IPython中运行上面的代码,那么您可以axes.spines['bottom'].set_position?查看所有选项.

是的.这不是一件容易的事,但你可以近距离接触.
bmu*_*bmu 33
matplotlib> = 1.4支持样式(并且内置了ggplot-style):
In [1]: import matplotlib as mpl
In [2]: import matplotlib.pyplot as plt
In [3]: import numpy as np
In [4]: mpl.style.available
Out[4]: [u'dark_background', u'grayscale', u'ggplot']
In [5]: mpl.style.use('ggplot')
In [6]: plt.hist(np.random.randn(100000))
Out[6]:
...
Run Code Online (Sandbox Code Playgroud)

gca*_*tes 28
######
编辑10/14/2013:有关信息,ggplot现已实现为python(基于matplotlib构建).
有关更多信息和示例,请参阅此博客或直接访问项目的github页面.
######
据我所知,matplotlib中没有内置的解决方案,可以直接为您的人物提供与R制作的相似的外观.
一些软件包(如mpltools)使用Matplotlib的rc参数添加了对样式表的支持,并且可以帮助您获得ggplot外观(请参阅ggplot样式以获取示例).
但是,由于所有内容都可以在matplotlib中进行调整,因此您可以更轻松地直接开发自己的函数来实现您想要的效果.例如,下面是一个片段,可以让您轻松自定义任何matplotlib图的轴.
def customaxis(ax, c_left='k', c_bottom='k', c_right='none', c_top='none',
lw=3, size=20, pad=8):
for c_spine, spine in zip([c_left, c_bottom, c_right, c_top],
['left', 'bottom', 'right', 'top']):
if c_spine != 'none':
ax.spines[spine].set_color(c_spine)
ax.spines[spine].set_linewidth(lw)
else:
ax.spines[spine].set_color('none')
if (c_bottom == 'none') & (c_top == 'none'): # no bottom and no top
ax.xaxis.set_ticks_position('none')
elif (c_bottom != 'none') & (c_top != 'none'): # bottom and top
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_bottom, labelsize=size, pad=pad)
elif (c_bottom != 'none') & (c_top == 'none'): # bottom but not top
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_bottom, labelsize=size, pad=pad)
elif (c_bottom == 'none') & (c_top != 'none'): # no bottom but top
ax.xaxis.set_ticks_position('top')
ax.tick_params(axis='x', direction='out', width=lw, length=7,
color=c_top, labelsize=size, pad=pad)
if (c_left == 'none') & (c_right == 'none'): # no left and no right
ax.yaxis.set_ticks_position('none')
elif (c_left != 'none') & (c_right != 'none'): # left and right
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_left, labelsize=size, pad=pad)
elif (c_left != 'none') & (c_right == 'none'): # left but not right
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_left, labelsize=size, pad=pad)
elif (c_left == 'none') & (c_right != 'none'): # no left but right
ax.yaxis.set_ticks_position('right')
ax.tick_params(axis='y', direction='out', width=lw, length=7,
color=c_right, labelsize=size, pad=pad)
Run Code Online (Sandbox Code Playgroud)
编辑:对于非接触刺,请参阅下面的函数,该函数引起10点位移的脊柱(取自matplotlib网站上的此示例).
def adjust_spines(ax,spines):
for loc, spine in ax.spines.items():
if loc in spines:
spine.set_position(('outward',10)) # outward by 10 points
spine.set_smart_bounds(True)
else:
spine.set_color('none') # don't draw spine
Run Code Online (Sandbox Code Playgroud)
例如,下面的代码和两个图表显示了matplotib(左侧)的默认输出,以及调用函数时的输出(右侧):
import numpy as np
import matplotlib.pyplot as plt
fig,(ax1,ax2) = plt.subplots(figsize=(8,5), ncols=2)
ax1.plot(np.random.rand(20), np.random.rand(20), 'ok')
ax2.plot(np.random.rand(20), np.random.rand(20), 'ok')
customaxis(ax2) # remove top and right spines, ticks out
adjust_spines(ax2, ['left', 'bottom']) # non touching spines
plt.show()
Run Code Online (Sandbox Code Playgroud)

当然,你需要时间来确定哪些参数必须在matplotlib中进行调整,以使你的图看起来与R图完全一样,但我不确定现在还有其他选项.
| 归档时间: |
|
| 查看次数: |
27689 次 |
| 最近记录: |