[英]Pandas Graph Bar and Line plot problems
我正在嘗試在條形圖的頂部繪制折線圖,以便從數據框中進行分析。 每當我嘗試添加折線圖時,由於某些原因,右側的y軸都會變成干草,而x軸上的條形圖標題會從正確變為字母順序。 我希望右側的y軸合理,如果可能的話,將直線弄直,下面是添加線后的條形圖,我試圖在x上繪制索引值,x是城鎮標簽,城鎮(城市)在左側y軸上,人口在右側。
首先應該是貝爾法斯特,然后是倫敦德里。
如果有人可以幫助,請多加贊賞。
x1= CitySample2017["index"]
y1= CitySample2017["Town/City"]
y2= CitySample2017["Population"]
ax1= CitySample2017.plot.bar(y="Town/City", x='index')
ax2 = ax1.twinx()
ax2.plot(x1, y2)
您正在使用matplotlib 2.1。 升級到matplotlib 2.2或更高版本,該代碼將按預期工作。
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"index" : ["Belfast", "London", "Twoabbey", "Lisboa", "Barra"],
"town" : [5000,1000,600,600,500],
"pop" : [12,14,16,18,20]})
ax1= df.plot.bar(y="town", x='index')
ax2 = ax1.twinx()
ax2.plot(df["index"], df["pop"])
plt.show()
我無法確定是否看不到您的數據,但是請嘗試運行此代碼而不是代碼:
ax1 = CitySample2017.plot.bar(x='index', y='Town/City')
ax2 = ax1.twinx()
CitySample2017.plot(x='index', y='Population', ax=ax2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.