0

目前我可以用 matplotlib 绘制一个最小和最大间隔,但是我希望对 plotnine 库做同样的事情,这可能吗?我正在尝试但没有成功

Matplotlib 代码

plot_range = list(range(0,len(sorted_table)))
min_val = sorted_table[['min']]
max_val = sorted_table[['max']]
original = sorted_table[['true']]
fig = plt.figure(1, figsize = (18,10)) # Figure size in inches (size_x, size_y)
ax = plt.axes()
plt.plot(plot_range, min_val, label = "Min", color='blue')
plt.plot(plot_range, max_val, label = "Max", color='red')
plt.plot(plot_range,original, label = "y", color = "black")
#plt.plot(static_predicted, label = "y\u0302", marker='o', )
plt.title('Static Final Conformal Predictions', fontsize=20)
plt.xlim([-2, 100])
plt.ylim([-1, 1.5])
plt.legend()
plt.show()

在此处输入图像描述

尝试 Plotnine 代码

import plotnine as p9
p9.options.set_option("figure_size", (16, 8))
(p9.ggplot(sorted_table, p9.aes(x = "index")
 + p9.geom_line(p9.aes(y = "min"), color = "blue")
 + p9.geom_line(p9.aes(y = "max"), color = "red")
 + p9.geom_line(p9.aes(y = "true"), color = "black")
))

PlotnineError: "Cannot add layer to object of type <class 'plotnine.aes.aes'>"
4

2 回答 2

1

这只是一个放错位置的右括号

(p9.ggplot(sorted_table, p9.aes(x = "index"))  # here
 + p9.geom_line(p9.aes(y = "min"), color = "blue")
 + p9.geom_line(p9.aes(y = "max"), color = "red")
 + p9.geom_line(p9.aes(y = "true"), color = "black")
)  # not here
于 2020-08-14T12:38:51.867 回答
1

使用带有整洁数据的 plotnine 几乎总是最好的。这样,您无需与绘图系统抗争即可获得传奇。

from plotnine import *
import pandas as pd
import numpy as np
import pandas.api.types as pdtypes

# Create sample data
x = np.arange(5)
sorted_data = pd.DataFrame({
    'min': x - 0.5,
    'true': x,
    'max': x + 0.5,
})

# Convert to tidy data format, making sure to keep the index
sorted_data_long = sorted_data.melt(value_vars=['min', 'true', 'max'], ignore_index=False).reset_index()

# Make variable a categorical, with categories ordered so as to make sense in the legend
sorted_data_long['variable'] = sorted_data_long['variable'].astype(pdtypes.CategoricalDtype(['min', 'true', 'max']))

# Plot
(ggplot(sorted_data_long, aes('index', y='value', color='variable'))
 + geom_line())

在此处输入图像描述

于 2020-08-14T20:55:31.593 回答