Python Forum
Plot Line chart based on the input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot Line chart based on the input
#1
Having set of unknow data , need to plot Line chart based on three params
Example
Salesamount = [[28000.00,308000.00,582000.00,387000.00],[367000.00,3037000.00,4822000.00,2936000.00],[637000.00,3440000.00,4415000.00,3294000.00],[478000.00,1209000.00,478000.00,1209000.00]]
CalendarYear = [2010,2011,2012,2013]
Employee = [272,281,282,296]
Here X value=CalendarYear,Y value=Salesamount, Labels =CalendarYear
but while plotting line chart it is saying as we need to have same set of X value and Y value

line, = axes.plot(xdata, ydata, 'r-')
Error :
ValueError: x and y must have same first dimension, but have shapes (4,) and (4,4)

Thanks for your help.
Reply
#2
May be something like this should help you (not tested):

fig = plt.figure()
ax = fig.add_subplot(111)
for y, c in zip(Salesamount, 'rgbk'):
    ax.plot(CalendarYear, y, c)
plt.show()
Reply
#3
Yes it did worked for fixed no of records.
As i stated earlier having N number of records, so i can't hardcode value as 'rgbk', there are more records present.
Reply
#4
You can try with longer string of colors, or
use cycle generator
from itertools import cycle
# 'rgbk' replace with cycle('rgbk')
Reply
#5
To plot a line chart with varying x and y values, you can plot each line separately using the plot function in a loop. Here's an example code snippet that should work with your input data:
import numpy as np
import matplotlib.pyplot as plt

Salesamount = [[28000.00,308000.00,582000.00,387000.00],[367000.00,3037000.00,4822000.00,2936000.00],[637000.00,3440000.00,4415000.00,3294000.00],[478000.00,1209000.00,478000.00,1209000.00]]
CalendarYear = [2010,2011,2012,2013]
Employee = [272,281,282,296]

fig, ax = plt.subplots()

for i in range(len(Employee)):
    ax.plot(CalendarYear, Salesamount[i], label=str(Employee[i]))

ax.legend(title='Employee')
ax.set_xlabel('Year')
ax.set_ylabel('Sales Amount')
ax.set_title('Sales Trend')

plt.show()
This code loops through each set of sales amount data for each employee and plots it using the plot function with the corresponding calendar year values. The label parameter is set to the employee ID for each line, and a legend is added to the plot to identify each line.

The resulting plot should have the x-axis labeled as "Year", the y-axis labeled as "Sales Amount", and a title of "Sales Trend". Each line should be identified in the legend by the corresponding employee ID.

I hope this helps!
buran write Apr-09-2023, 04:55 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a plot with line graphs and vertical bars devansing 6 2,251 Feb-28-2023, 05:38 PM
Last Post: devansing
  Pick a line in a plot with matplotlib Romain 3 5,538 Feb-28-2023, 11:56 AM
Last Post: get2sid
  Help to Plot timeline for intreruption of one line production danut_horincas 2 2,505 Feb-28-2023, 11:48 AM
Last Post: get2sid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020