我想知道一个人如何旋转x标签,类似于以下内容:
theme(axis.text.x = element_text(angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
在ggplot中?
谢谢。
我一直是 R 的长期用户,最近过渡到 Python,并且我一直在尝试使用 ggplot2 进行绘图,因为它非常直观。Plotnine被认为是最 ggplot2 风格的绘图库,我已经成功地用它重新创建了大多数图形,除了关键的是如何绘制常规函数。
在基础 R 中,您可以轻松定义一个方程,如so,将结果输入到一个stat_function()
层中,并设置图形的限制代替data
arg.,并成功绘制抛物线等。但是,设置图形限制的语法在 Python 中必须不同(也许使用 numpy?),并且使用 定义方程sympy
,这对我来说是另一个分歧。
那么我将如何使用 plotnine 绘制函数呢?以上两个障碍是我认为给我带来麻烦的与 ggplot2 的两个不同之处,因为 plotnine 在网上的例子太少了。
PS 这是我想使用 plotnine 在 Python 中重新创建的示例:
> library(ggplot2)
> basic_plot <- function(x) x^2 + 2.5
> graph <- ggplot(data.frame(x=c(-5,5)), aes(x=x)) +
+ stat_function(fun = basic_plot)
> graph
Run Code Online (Sandbox Code Playgroud) 我正在尝试调整 geom_text 标签,使它们不重叠。使用 nudge_x、nudge_y 和 size 调整标签并没有给我一个非常满意的结果。我遇到了 adjustment_text 包,但我无法将其实现到我的代码中。
现在我的图表看起来像这样:
我尝试添加 adjustment_text =True (见下文),这给了我以下错误。“几何、统计或图层都无法理解参数{'调整文本'}。”
(ggplot(nutrient_rel_item, aes(y='Share_calories', x='Share_biomass', color='Type',show_legend=False))
+stat_smooth( aes(y='Share_calories', x='Share_biomass'),method='lm',inherit_aes=False)
+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)
+facet_wrap('~CCC1',nrow=2)
+ scale_x_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ scale_y_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ geom_point(size=0.1)
+ geom_path(aes(group='Item'), arrow= arrow(angle = 15, length= 0.1, type = "closed"))
+ labs(x='x', y='y')
+ theme(legend_position='none'))
Run Code Online (Sandbox Code Playgroud) 我使用plotnine生成散点图,其中x 轴是pandas.Timestamp 对象。
目前,x 刻度标签(例如“2017-07-01”)正在相互冲突。我希望能够对刻度标签进行任意转换。如何更改绘图上的 x 刻度标签?
看起来我可以做类似的事情+ scale_x_continuous(labels=???)
,但我不知道要传递给标签什么参数。
我想在plotnine 中的一行添加一个标签。使用 geom_text 时出现以下错误:
'NoneType' object has no attribute 'copy'
Run Code Online (Sandbox Code Playgroud)
示例代码如下:
df = pd.DataFrame({
'date':pd.date_range(start='1/1/1996', periods=4*25, freq='Q'),
'small': pd.Series([0.035]).repeat(4*25) ,
'large': pd.Series([0.09]).repeat(4*25),
})
fig1 = (ggplot()
+ geom_step(df, aes(x='date', y='small'))
+ geom_step(df, aes(x='date', y='large'))
+ scale_x_datetime(labels=date_format('%Y'))
+ scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ labs(x=None, y=None)
+ geom_text(aes(x=pd.Timestamp('2000-01-01'), y = 0.0275, label = 'small'))
)
print(fig1)
Run Code Online (Sandbox Code Playgroud)
编辑:
下面 has2k1 的答案解决了该错误,但我得到:
我想要这个:(来自R)
R代码:
ggplot() +
geom_step(data=df, aes(x=date, y=small), color='#117DCF', size=0.75) +
geom_step(data=df, aes(x=date, y=large), color='#FF7605', size=0.75) …
Run Code Online (Sandbox Code Playgroud) 我已经使用 ggplot 很长时间了,并且在 R 中使用它非常舒服。我现在正在学校使用 Python,我最难理解这个错误。当我尝试使用 scale_color_manual 将颜色手动分配给名为“CellTypeOther”的具有唯一值 0/1 的变量时,我不断收到以下错误:
NameError: name 'c' is not defined
Run Code Online (Sandbox Code Playgroud)
这是我创建 ggplot 的代码:
from plotnine import *
plot = (
ggplot(Y_tsne,aes(x = 'X',y = 'Y'))
+ geom_point(aes(color = 'CellTypeOther'),alpha = 0.4,size = 2)
+ scale_color_manual(c("blue","red"))
)
Run Code Online (Sandbox Code Playgroud)
没有最后一行,情节渲染得很好。有没有人对可能发生的事情有任何线索?我不能分享我的数据,因为它很敏感。
重要说明:我正在使用 plotnine Python 模块来利用 ggplot2。
我一直在尝试使用plotnine 进行绘图,据我所知,它是Python 中最像ggplot2 风格的库,但我很难在Python 脚本中的PyCharm 中完成它。这似乎是一个问题,对于 matplotlib 和其他也遇到此错误的绘图库有许多解决方案,但使用他们的答案(添加DISPLAY=True
到环境变量,show()
在图末尾添加等)不适用于plotnine。
我正在尝试绘制以下内容,它在常规 ipython 窗口(我通过终端访问)中工作:
from plotnine import *
from plotnine.data import *
(ggplot(diamonds)
+ stat_summary(aes('clarity', 'price', fill='cut'), geom='col', position='dodge')
+ stat_summary(aes('clarity', 'price', color='cut'), geom='linerange', size=1, position=position_dodge(width=0.9))
+ scale_color_hue(l=.3)
+ theme(figure_size=(11, 8))
)
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么,或者我不应该尝试从脚本中进行绘图,这通常对其他一切都很方便?
PS 解决方案:这似乎是 PyCharm 中的一个错误,社区可能还没有注意到,但这有点公平,因为plotnine 库也有点新。我的解决方案是避免尝试在脚本中执行此操作,而是从 PyCharm 中的 ipython 控制台内进行绘图,这是有效的。
如何将其拟合为非线性曲线?
def plot(version, str_metric):
metric1 = [0.459, 0.5844, 0.6246, 0.6459, 0.6429, 0.6629, 0.664, 0.6722, 0.6832, 0.6931, 0.7103, 0.7144, 0.7213, 0.716, 0.7182, 0.7419, 0.7418, 0.7594, 0.7639, 0.7854, 0.7752, 0.7912, 0.8164, 0.8311,
0.8283, 0.8103, 0.869, 0.8708, 0.884, 0.9017, 0.9248, 0.9233, 0.9432, 0.9306, 0.945, 0.959, 0.9675, 0.9649, 0.982, 0.9276, 0.975, 0.9772, 0.9786, 0.988, 0.9825, 0.9901, 0.9934, 0.993, 0.9956, 0.9951]
metric2 = [0.4541, 0.5807, 0.6138, 0.6339, 0.6322, 0.63, 0.6451, 0.6514, 0.6589, 0.6597, 0.6759, 0.6817, 0.6866, 0.6803, 0.6819, 0.7009, 0.7039, 0.7162, 0.7197, 0.7237, …
Run Code Online (Sandbox Code Playgroud)