Streamlit 上的 Pyplot 功能出现问题

问题描述 投票:0回答:3
#pie chart
import pylab
df_count_percent = (df_count['count_of_purchase/campaign'] / df_count['count_of_purchase/campaign'].sum()) * 100
plot0 = df_count_percent.plot.pie(y='count_of_purchase/campaign', fontsize=17,autopct='%0.2f%%',pctdistance=1.2,
                                     labeldistance=None)

pylab.ylabel('')
pylab.xlabel('')

plt.title('Count of Purchase or Campaign as Percentage (Pie)', fontsize=19,fontweight="bold",pad=6)

plt.legend(loc='upper right', fontsize=9,prop={'size': 17}, bbox_to_anchor=(2,1))
plot0=plt.figure(figsize=(16, 16))
plt.show()

# Plot a bar chart
figure_0=df_count.plot.barh( y="count_of_purchase/campaign",
         color='green',fontsize=15,edgecolor="#111111")

plt.title('Count of Purchase or Campaign', fontsize=16,fontweight="bold",pad=12)
figure_0.set_xlabel('\nFrequency',fontsize=14,fontweight="bold")
figure_0=plt.figure()
plt.show()
# Plot a bar chart as percentage
df_count_percent = (df_count['count_of_purchase/campaign'] / df_count['count_of_purchase/campaign'].sum()) * 100
fig=df_count_percent.plot.barh( y="count_of_purchase/campaign",fontsize=15 ,color='red',edgecolor="#111111")

plt.title('Count of Purchase or Campaign as Percentage',fontsize=16,fontweight="bold",pad=12) 

fig.set_xlabel('\nPercentage',fontsize=14,fontweight="bold")
fig = plt.figure(figsize = (18, 18))
plt.show()

朋友们大家好,我正在尝试在streamlit上绘制上述三个图表(饼图和条形图),但是饼图和

y=count_of_purchase/campaign
图是空白的。我尝试了以下代码用于streamlit:
st.pyplot(fig)
我怎样才能在streamlit上真正绘制这些?有什么问题,我不知道?通常,代码可以在spyder/jupyter上运行,但问题出在接口上。我无法在 Streamlit 上显示它。我正在等待您的帮助...

python-3.x matplotlib plot streamlit
3个回答
0
投票

使用

plt.show()
,你的绘图将显示在与streamlit无关的不同窗口中。为了解决这个问题,您必须将所有
plt.show()
替换为
st.write()

import streamlit as st

# First plot
st.write(plot0)

# Second plot
st.write(figure_0)

# Third plot
st.write(fig)

0
投票

st.pyplot 需要一个 Matplotlib Figure 对象,并且可以通过向 df.plot.barh(stacked=True) 添加 .figure 属性来访问该对象:

import pylab
df_count_percent = (df_count['count_of_purchase/campaign'] / df_count['count_of_purchase/campaign'].sum()) * 100
plot0 = df_count_percent.plot.pie(y='count_of_purchase/campaign', fontsize=17,figsize=(16, 16),autopct='%0.2f%%',pctdistance=1.2,
                                   labeldistance=None,stacked=True)

pylab.ylabel('')
pylab.xlabel('')

plt.title('Count of Purchase or Campaign as Percentage (Pie)', fontsize=19,fontweight="bold",pad=6)

plt.legend(loc='upper right', fontsize=9,prop={'size': 17}, bbox_to_anchor=(2,1))
  
plot0=plot0.figure
st.pyplot(plot0)

0
投票

您需要确保将

Figure
对象传递给
st.pyplot
这是一个示例,展示了如何一次绘制多个图像。

enter image description here

这是一个生成

Figure
对象的示例函数:

def generate_random_plot():
    # Generate random data
    x = np.linspace(0, 10, 100)
    y = np.random.rand(100)
    color = np.random.rand(3,)  # Random color for the line

    # Randomly choose between histogram and scatterplot
    plot_type = np.random.choice(['scatter', 'histogram'])

    if plot_type == 'scatter':
        # Create a scatter plot
        fig, ax = plt.subplots()
        ax.scatter(x, y, color=color, label='Random Scatter Data')
        ax.set_title('Random Scatter Plot')
    else:
        # Create a histogram
        fig, ax = plt.subplots()
        ax.hist(y, bins=10, color=color, label='Random Histogram Data', alpha=0.7)
        ax.set_title('Random Histogram Plot')

    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
    ax.legend()
    return fig

© www.soinside.com 2019 - 2024. All rights reserved.