首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Plotnine绘制最小和最大间隔?

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

Stack Overflow用户
提问于 2020-08-14 17:55:01
回答 2查看 147关注 0票数 0

目前我可以用matplotlib绘制最小和最大间隔,但是我希望用plot9库做同样的事情,这可能吗?我在努力,但没有成功

Matplotlib代码

代码语言:javascript
复制
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代码

代码语言:javascript
复制
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'>"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-15 04:55:32

tidy data中使用plotnine几乎总是最好的。这样,你就可以获得一个图例,而不会与绘图系统发生冲突。

代码语言:javascript
复制
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())

票数 1
EN

Stack Overflow用户

发布于 2020-08-14 20:38:52

这只是一个放错地方的闭括号

代码语言:javascript
复制
(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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63410565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档