[英]How to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot?
[英]matplotlib plot line and bar chart together on same x-axis
當我將兩個 Pandas dfs 繪制成兩個折線圖時,我將它們正確地放在同一個 x 軸上。 但是,當我將其繪制為條形圖時,軸似乎偏移了。
ax = names_df.loc[:, name].plot(color='black')
living_df.loc[:, name].plot(figsize=(12, 8), ax=ax)
這工作正常,產生這個結果
另一方面,這:
ax = names_df.loc[:, name].plot(color='black')
living_df.loc[:, name].plot.bar(figsize=(12, 8), ax=ax)
沒有,並且有這個結果
使用matplotlib
而不是調用 pandas 對象的plot
方法:
import matplotlib.pyplot as plt
# Line plot
plt.plot(names_df.loc[:, name], color='black')
plt.plot(living_df.loc[:, name])
plt.show()
plt.close()
# Bar plot
plt.plot(names_df.loc[:, name].values)
bar_data = living_df.loc[:, name].values
plt.bar(range(len(bar_data)), bar_data)
plt.xticks(range(len(bar_data)), names_df.index.values) # Restore xticks
plt.show()
plt.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.