Python 当使用辅助工具时,绘图会发生偏移
我想用两个y轴在同一个图中绘制一个气象站的温度和降水量。然而,当我尝试这一点时,其中一个情节会无缘无故地发生变化。这是我的代码:到目前为止,我刚刚尝试了两次降水测量,但你得到了交易Python 当使用辅助工具时,绘图会发生偏移,python,matplotlib,Python,Matplotlib,我想用两个y轴在同一个图中绘制一个气象站的温度和降水量。然而,当我尝试这一点时,其中一个情节会无缘无故地发生变化。这是我的代码:到目前为止,我刚刚尝试了两次降水测量,但你得到了交易 ax = m_prec_ra.plot() ax2 = m_prec_po.plot(kind='bar',secondary_y=True,ax=ax) ax.set_xlabel('Times') ax.set_ylabel('Left axes label') ax2.set_ylabel('Right axe
ax = m_prec_ra.plot()
ax2 = m_prec_po.plot(kind='bar',secondary_y=True,ax=ax)
ax.set_xlabel('Times')
ax.set_ylabel('Left axes label')
ax2.set_ylabel('Right axes label')
这将返回以下绘图:
我看到有人问同样的问题,但我似乎不知道如何手动移动我的一个数据集。
以下是我的数据:
打印预检表,预检单
Time
1 0.593436
2 0.532058
3 0.676219
4 1.780795
5 4.956048
6 11.909394
7 17.820051
8 14.225257
9 10.261061
10 2.628336
11 0.240568
12 0.431227
Name: Precipitation (mm), dtype: float64 Time
1 0.704339
2 1.225169
3 1.905223
4 4.156270
5 11.531221
6 22.246230
7 30.133800
8 27.634639
9 20.693056
10 5.282412
11 0.659365
12 0.622562
Name: Precipitation (mm), dtype: float64
您在绘图时使用的熊猫版本是什么 使用0.23.4运行此代码:
df1 = pd.DataFrame({'Data_1':[1,2,4,8,16,12,8,4,1]})
df2 = pd.DataFrame({'Data_2':[1,2,4,8,16,12,8,4,1]})
ax = df1.plot()
ax2 = df2.plot(kind='bar',secondary_y=True,ax=ax)
ax.set_xlabel('Times')
ax.set_ylabel('Left axes label')
ax2.set_ylabel('Right axes label')
我得到:
如果您想添加示例数据,我们可以查看该数据。有关此行为的解释,请参见 在这里,解决方案是将线向前移动一行,即根据从0开始的索引(而不是1)进行绘图
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"A" : np.arange(1,11),
"B" : np.random.rand(10),
"C" : np.random.rand(10)})
df.set_index("A", inplace=True)
ax = df.plot(y='B', kind = 'bar', legend = False)
df2 = df.reset_index()
df2.plot(ax = ax, secondary_y = True, y = 'B', kind = 'line')
plt.show()
另一个问题在哪里?它的答案在多大程度上没有帮助?这里:答案表明我改变了数据,但我似乎无法让这种情况发生。该答案不包含任何代码,因为该问题未提供用于显示示例案例的任何数据;这个问题也没有。我现在用数据更新了我的问题