Home > Software engineering >  Python Matplotlib plot multiple data in single graph
Python Matplotlib plot multiple data in single graph

Time:12-30

I am trying to find a meaningful way to plot all the data in a single graph.

my data kind of look like this after all transformations -

Stock Date Action Quantity Traded %
Arihant Tournesol Ltd. 2022-12-27 Disposal 2836900.0 28.660477
Asit C Mehta Financial Services Ltd. 2022-12-28 Disposal 380000.0 7.839254
HCKK Ventures Ltd. 2022-12-28 Acquisition 1866917.0 50.321213
HCKK Ventures Ltd. 2022-12-28 Disposal 1866917.0 50.321213
Mafatlal Industries Ltd. 2022-12-27 Acquisition 11500000.0 16.316917
Mafatlal Industries Ltd. 2022-12-27 Disposal 11500000.0 16.316917
Milgrey Finance & Investments Ltd. 2022-12-28 Revoke 126000.0 6.331658
Shreeshay Engineers Ltd. 2022-12-26 Disposal 2460000.0 18.631565
Suumaya Corporation Ltd. 2022-12-27 Pledge 10108008.0 40.885038
Yarn Syndicate Ltd. 2022-12-28 Disposal 330000.0 8.800000
normal plotting will be like 
df.plot(kind='bar', x='Stock', y='Quantity', color='olivedrab')

but this will give a 2-data plot, I am trying to get all the data plotted on single graph

another way it can be done -

ax = df.plot(kind='bar', x='Stock', y='Quantity', 
               figsize=(16, 8), color='olivedrab', 
               [enter image description here][1])
ax1 = df['Traded %'].plot(secondary_y=True, ax=ax, color='indianred')

ax.tick_params(axis='x', labelsize=14, rotation=90)
plt.show()

this generates a pic like this, please check the link - but i am missing the date. [1]: https://i.stack.imgur.com/9VNYC.jpg

Please, if you have any other way to plot them in a single graph, please share...

CodePudding user response:

To plot multiple datasets on the same graph you can try as bellow:

# Import the Matplotlib library
import matplotlib.pyplot as plt

# Prepare the data for the first dataset
x1 = [1, 2, 3, 4]  # x-values
y1 = [10, 20, 25, 30]  # y-values

# Prepare the data for the second dataset
x2 = [1, 2, 3, 4]  # x-values
y2 = [5, 10, 15, 20]  # y-values

# Create a figure and an axis
fig, ax = plt.subplots()

# Plot the first dataset on the axis
ax.plot(x1, y1,  # x and y values
        color='r',  # line color
        linestyle='--',  # line style
        label='dataset 1')  # label for the legend

# Plot the second dataset on the axis
ax.plot(x2, y2,  # x and y values
        color='b',  # line color
        linestyle='-',  # line style
        label='dataset 2')  # label for the legend

# Add a legend to the plot
ax.legend()

# Add labels to the x and y axes
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')

# Add a title to the plot
ax.set_title('Multiple datasets')

# Show the plot
plt.show()

This will create a graph with two lines, one for each dataset, with different colors and line styles.

The legend will label each line with the corresponding dataset.

CodePudding user response:

Try this:

import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

ax1 = sns.set_style(style=None, rc=None )

fig, ax1 = plt.subplots(figsize=(12,6))

sns.lineplot(data = df,x='Stock', y='Traded', marker='o', sort = False, ax=ax1)
ax2 = ax1.twinx()

sns.barplot(data = df, x='Stock', y='Quantity', alpha=0.5, ax=ax2)
  • Related