我正在尝试从 Python 中创建每小时热图。此处提供了使用 R 制作的此图的代码示例:r-graph-gallery.com/283-the-hourly-heatmap.html。它依赖于ggplot2。
还有一个 ggplot2 的 Python 实现,称为 plotnine:github.com/has2k1/plotnine
任何人都能够从 R 到 Python 的“翻译”?
我正在尝试从 Python 中创建每小时热图。此处提供了使用 R 制作的此图的代码示例:r-graph-gallery.com/283-the-hourly-heatmap.html。它依赖于ggplot2。
还有一个 ggplot2 的 Python 实现,称为 plotnine:github.com/has2k1/plotnine
任何人都能够从 R 到 Python 的“翻译”?
这是使用小型数据集的plotnine
文档中的简单娱乐。来自的大多数geom_*
元素ggplot2
都已实现(主要用下划线替换名称中的点)。如果要保持R
直接调用函数的风格,geom_*
只需将第一行更改为from plotnine import *
.
import plotnine as p9
import pandas as pd
import numpy as np
#%% Build dataset
df = pd.DataFrame({'Month':pd.Categorical(['Jan','Feb','Mar']*2*3*4,
categories=['Jan','Feb','Mar']),
'Year':([2004]*3+[2005]*3)*3*4,
'Hour Commencing':([1]*3*2+[2]*3*2+[3]*3*2)*4,
'Day':[j for i in range(1,5) for j in [i]*3*2*3]})
# Add the hourly temp as random values
df['Hourly Temps'] = np.random.randint(-10,40,size=df.shape[0])
#%% Build the plot
g = (p9.ggplot(df,p9.aes(x='Day',y='Hour Commencing',fill='Hourly Temps'))
+ p9.geom_tile(color='white',size=.1)
+ p9.facet_grid('Year~Month')
+ p9.labs(title='Hourly Temps - Station T0001')
+ p9.scale_fill_cmap('plasma')
+ p9.theme(legend_position = 'bottom',
plot_title = p9.element_text(size=14),
axis_text_y = p9.element_text(size=6)))
print(g)