用Plotnine绘制最小和最大间隔?

问题描述

当前,我能够使用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()

enter image description here

尝试使用拼写代码

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'>"

解决方法

这只是放错位置的右括号

(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
,

几乎总是最好将{> {3}}与普乐汀一起使用。这样,您就可以在不与绘图系统抗争的情况下获得图例。

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','max']))

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

tidy data