简体   繁体   English

如何在绘图pandas或matplotlib python中停止折线图

[英]How to discontinue a line graph in the plot pandas or matplotlib python

My challenge is to plot many sequences of data organized in the column (where each column is the data for many simualtions for the same identificator (ID)) and index of pandas dataframe is the months of simulation. 我的挑战是绘制列中组织的许多数据序列(其中每列是同一标识符(ID)的许多模拟的数据),并且pandas数据帧的索引是模拟的月份。 The problem is in the line created by pandas linking the different simulations in the same column. 问题出在pandas创建的行中,链接同一列中的不同模拟。

Look at the example which reproduces the problem. 看一下再现问题的例子。 How can I fix it? 我该如何解决?

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

# create da dataset
columns = ['A','B']
data = np.array([np.random.randint(10, size=15),
             np.random.randint(10, size=15)]).T
index = list(range(0,5))*3
dataset = pd.DataFrame(data, index=index, columns=columns)

# plotting 
plot_data = dataset.plot(title='Example StackOverflow')
plot_data.set_xlabel('Years')
plot_data.set_ylabel('Values')
plot_data.legend(loc='best', ncol=4, fancybox=True, shadow=True)
plot_data.set_axis_bgcolor('w')
fig = plot_data.get_figure()
fig.savefig('example_figure_stackoverflow.png', dpi=400)

result 结果

绘制结果作为链接线的问题

Here a solution that directly uses matplotlib: 这里有一个直接使用matplotlib的解决方案:

# code until "plotting" same as question

# plotting
simlen = 5
for c in columns:
  for i in range(0, len(index), simlen):
    plt.plot(index[i:i+simlen], dataset[i:i+simlen][c],
      color=dict(A='b', B='g')[c],
      label=c if i == 0 else None)
plt.legend()
plt.show()

(I assumed each simulation has length 5, which was not explicit in your question. Note that the data could be structured differently since pandas is no longer used for plotting.) (我假设每个模拟的长度都是5,这在你的问题中并不明确。请注意,由于大熊猫不再用于绘图,因此数据的结构可能不同。)

Here the output: 这里输出: 样本数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM