How to terminate a line graph in pandas or matplotlib python graph

My task is to build many sequences of data organized in a column (where each column is the data for many simulations for a single identifier (ID)), and the pandas index is months of modeling. The problem is the row created by pandas linking the various simulations in the same column.

Look at an example that reproduces the problem. How can i fix this?

# 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

Graph Result as a Line Linking Problem

+5
source share
1 answer

Here is a solution that directly uses 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 that each simulation has a length of 5, which was not explicit in your question. Note that the data may be structured differently since pandas is no longer used for construction.)

Here's the conclusion: sample figure

+1
source

All Articles