I am currently developing the app review analyzer.
When I plot the line chart or pie chart, the date x-axis worked well, but not for the bar-chart.
the time axis for the bar chart figure always starts from 1970-01-01 which is not intended.
my code is,
def plot_review_trends(df, clusters, occurrences):
# Ensure 'date' column is in datetime format
df['date'] = pd.to_datetime(df['date'])
fig, axes = plt.subplots(2, 2, figsize=(15, 15))
fig.subplots_adjust(hspace=0.6, wspace=0.4, top=0.9, bottom=0.1, left=0.1, right=0.9)
fig.suptitle('Monthly App Review Report', fontsize=24)
# Bar plot for star distribution
colors = sns.color_palette("pastel")
ax1 = axes[0, 0]
ax2 = ax1.twinx() # create a second y-axis
df[['date', '1-star', '2-star', '3-star', '4-star', '5-star']].plot(kind='bar', x='date', stacked=True,
ax=ax1, color=colors)
# Line plot for average and trend
df['average'] = (df['1-star'] * 1 + df['2-star'] * 2 + df['3-star'] * 3 + df['4-star'] * 4 + df['5-star'] * 5) / df[
['1-star', '2-star', '3-star', '4-star', '5-star']].sum(axis=1)
ax2.plot(df.index, df['average'], color='blue', label='Average', linewidth=2)
# Calculate trend line
z = np.polyfit(range(len(df)), df['average'], 1)
p = np.poly1d(z)
ax2.plot(df.index, p(range(len(df))), color='black', label='Trend', linewidth=2)
# Set legend explicitly
ax1.legend(['1-star', '2-star', '3-star', '4-star', '5-star'], loc='upper left')
ax2.legend(['Average', 'Trend'], loc='upper right')
# format
ax1.set_title('Star Distribution Over Time')
ax1.set_xlabel('Date')
ax1.set_ylabel('Number of Reviews')
ax2.set_ylabel('Average Rating')
# Correct date formatting
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.xaxis.set_major_locator(mdates.DayLocator())
ax1.tick_params(axis='x', rotation=45)
# Plot positive vs negative reviews without legend and with integrated pie chart
df['positive'] = df['5-star'] + df['4-star'] + df['3-star']
df['negative'] = df['1-star'] + df['2-star']
df[['date', 'positive', 'negative']].plot(kind='line', x='date', y=['positive', 'negative'], ax=axes[0, 1])
axes[0, 1].set_title('Positive vs Negative Reviews Over Time')
axes[0, 1].set_xlabel('Date')
axes[0, 1].set_ylabel('Number of Reviews')
axes[0, 1].legend().set_visible(False) # Remove legend
axes[0, 1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
axes[0, 1].xaxis.set_major_locator(mdates.DayLocator())
axes[0, 1].tick_params(axis='x', rotation=45)
# Create an inset pie chart within the positive vs negative plot
inset_ax = fig.add_axes([axes[0, 1].get_position().x1 - 0.17, axes[0, 1].get_position().y1 - 0.16, 0.15, 0.15])
df[['positive', 'negative']].sum().plot(kind='pie', autopct='%1.1f%%', ax=inset_ax, colors=['#66b3ff', '#ff9999'])
inset_ax.set_ylabel('')
# Plot pie chart for clusters
axes[1, 1].pie(occurrences, labels=clusters, autopct='%1.1f%%', startangle=140)
axes[1, 1].set_title('Clustered Feedback Analysis')
axes[1, 1].axis('equal')
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.savefig('app_review_report.png', bbox_inches='tight')
plt.show()
and it returned the something like,
which has a problem of x-axis labeling for first figure.
Is there anyone who can suggest me to solve this issue?
Thanks.