[英]How to get axes correct matplotlib
I have this table and I want to display two trends vs the date.我有这张表,我想显示两个趋势与日期。 I want to show the close of the stock prices from the table along with the oil prices.我想显示表格中股票价格的收盘价以及石油价格。 For some reason, I'm really struggling to do this.出于某种原因,我真的很难做到这一点。 Here's what I've done so far:这是我到目前为止所做的:
As you can clearly see, this is not what I'm going for... I don't know why my y axis appears this way and I just want to lines displaying these columns with the y axis as price.正如您可以清楚地看到的那样,这不是我想要的......我不知道为什么我的 y 轴会以这种方式出现,我只想用 y 轴作为价格显示这些列。 Can someone help?有人可以帮忙吗?
Here is the result of the info command on this table.这是该表上的 info 命令的结果。
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 91 entries, 2010-02-01 to 2020-09-01
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Open 91 non-null float64
1 High 91 non-null float64
2 Low 91 non-null float64
3 Close 91 non-null float64
4 Adj Close 91 non-null float64
5 Volume 91 non-null int64
6 Oil Price 91 non-null object
dtypes: float64(5), int64(1), object(1)
memory usage: 8.2+ KB
As you can see from the output of stocksAndOil.info()
, the column Close
is float64
type and this is fine: pandas interprets its values as numbers.从stocksAndOil.info()
的输出中可以看出, Close
列是float64
类型,这很好:pandas 将其值解释为数字。
On the contrary, the column Oil Price
is object
: this means pandas interprets its values as string, not numbers.相反,列Oil Price
is object
:这意味着 pandas 将其值解释为字符串,而不是数字。 In other words, the value 74.41
for Oil price
at 2020-02-01
(first row of the screenshot above) is interpreted as the string '74.41'
.换句话说,该值74.41
为Oil price
在2020-02-01
(上面的截图的第一行)被解释为字符串'74.41'
。
To solve, you need to convert Oil price
column type from str
to float
with:要解决,您需要将Oil price
列类型从str
转换为float
:
stocksAndOil['Oil price'] = stocksAndOil['Oil price].astype(float)
Check this code as an example:检查此代码作为示例:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'A': [1, 2, 3],
'B': ['3', '2', '4']})
print(df.info())
fig, ax = plt.subplots()
ax.scatter(df['A'], df['B'])
plt.show()
As you can se the column 'B'
has only str
values.正如您可以看到的,列'B'
只有str
值。 The output of df.info()
is: df.info()
的输出是:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null int64
1 B 3 non-null object
dtypes: int64(1), object(1)
memory usage: 100.0+ bytes
Ans the resulting plot is:得到的情节是:
Check the y axis labels: 2
is between 3
and 4
, so this is clearly wrong.检查 y 轴标签: 2
介于3
和4
之间,所以这显然是错误的。
To solve I use this code:为了解决我使用这个代码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'A': [1, 2, 3],
'B': ['3', '2', '4']})
df['B'] = df['B'].astype(float)
print(df.info())
fig, ax = plt.subplots()
ax.scatter(df['A'], df['B'])
plt.show()
The output of df.info()
is: df.info()
的输出是:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null int64
1 B 3 non-null float64
dtypes: float64(1), int64(1)
memory usage: 112.0 bytes
This time the 'B'
column has float
values and the resulting plot is correct:这次'B'
列具有float
值,结果图是正确的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.