Reputation: 488
Currently I am able to plot a min and max interval with matplotlib, however I wish to do the same with the plotnine library, would this be possible? I am trying but am not successfull
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()
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'>"
Upvotes: 0
Views: 580
Reputation: 2375
It is almost always best to use plotnine with tidy data. This way you get a legend without fighting with the plotting system.
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())
Upvotes: 1
Reputation: 1573
It's just a missplaced closing bracket
(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
Upvotes: 1