我得到了这个数据帧df,
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
table = {'Count_Product': pd.Series([1,2,3]), 'Count_Transaction': pd.Series([1,1,2])}
df = pd.DataFrame(table)
df
Count_Product Count_Transaction
0 1 1
1 2 1
2 3 2
我想做一个简单的条形图,其中x轴是Count_Product,y轴是Count_Transaction。我使用以下代码生成,
%matplotlib inline
ax = df['Count_Product'].plot(kind='bar', figsize=(5,5), color='blue')
ax.set_ylabel("Count_Transaction")
ax.set_xlabel("Count_Product")
patches, labels = ax.get_legend_handles_labels()
ax.legend(patches, labels, loc='best')
图表输出是,
我的问题是x轴在Count_Product列中显示0,1,2而不是1,2,3。它看起来像是自动将第1列作为x轴。
有谁知道我如何编写正确的列以使用Count_Product列作为x轴?
谢谢, Lobbie
答案 0 :(得分:3)
我认为您需要先从Count_Product
列开始set_index
,然后将df['Count_Product'].plot
更改为df['Count_Transaction'].plot
:
df = df.set_index('Count_Product')
ax = df['Count_Transaction'].plot(kind='bar', figsize=(5,5), color='blue')
ax.set_ylabel("Count_Transaction")
ax.set_xlabel("Count_Product")
patches, labels = ax.get_legend_handles_labels()
ax.legend(patches, labels, loc='best')