Python中图表的For循环

Python中图表的For循环,python,pandas,dataframe,for-loop,matplotlib,Python,Pandas,Dataframe,For Loop,Matplotlib,我想创建一个带有for循环的图表。正确绘制X轴,甚至正确设置y轴比例,但未创建实际折线图。 下面的代码可能有什么问题 import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib test = {'date':[2012, 2013, 2014, 2015],'val':[9,15,12,20]} test_df=pd.DataFrame(data=test) test_df

我想创建一个带有for循环的图表。正确绘制X轴,甚至正确设置y轴比例,但未创建实际折线图。 下面的代码可能有什么问题

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

test = {'date':[2012, 2013, 2014, 2015],'val':[9,15,12,20]}

test_df=pd.DataFrame(data=test)

test_df=test_df.set_index('date')

fig, ax = plt.subplots(figsize=(25,25))

for i in range(0, len(test_df)):
    
    ax.plot(test_df.index[i], test_df['val'].iloc[i], color='blue', linewidth=10)
        
plt.show()

您可以使用以下方法绘制整个系列:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)
对于循环

或者用以下方法绘制点本身:

ax.plot(test_df.index[i], test_df['val'].iloc[i], "b+", linewidth=10)

有关单个点标记的信息,请参见。您可以使用以下方法绘制整个系列:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)
对于循环

或者用以下方法绘制点本身:

ax.plot(test_df.index[i], test_df['val'].iloc[i], "b+", linewidth=10)

有关单个点的信息,请参见标记,标记不提供绘制线的坐标,这将导致我们绘制散点图

for i in range(0, len(test_df)): 
    ax.scatter(test_df.index[i], test_df['val'].iloc[i], color='blue', linewidth=10)
就我而言,这将解决你的问题,但如果你真的想连接这些点,那么你可以在不使用for循环的情况下进行尝试:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)

你不给坐标来画一条线,这会导致我们画一个散点图

for i in range(0, len(test_df)): 
    ax.scatter(test_df.index[i], test_df['val'].iloc[i], color='blue', linewidth=10)
就我而言,这将解决你的问题,但如果你真的想连接这些点,那么你可以在不使用for循环的情况下进行尝试:

ax.plot(test_df.index, test_df['val'], color='blue', linewidth=10)

谢谢,这为我解决了问题!“b+”本身不起作用,但其他标记也起作用。谢谢,这为我解决了问题!“b+”本身不起作用,但其他标记确实起作用。