OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to add a serie of data to plots inside a loop for? Matplotlib Python

  • Thread starter Thread starter Another.Chemist
  • Start date Start date
A

Another.Chemist

Guest
I am following this example.

I have this code:

Code:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
df = pd.read_csv('Test_1.csv',header=0,delimiter="\t")
x_col = df[['Ref']]
y_columns = df[['R2SCAN','Ref','TPSS0_D3BJ']]
# Crear las gráficas
for col in y_columns:
    plt.figure()
    plt.scatter(df["Ref"], df[col])
df = pd.read_csv('Test_2.csv',header=0,delimiter="\t")
# Crear las gráficas
for col in y_columns:
 #   plt.figure()
    plt.scatter(df["Ref"], df[col])
    plt.xlabel("DLPNO-CCSD(T)/CBS(3,4) [kcal/mol]")
    plt.ylabel(f'{col}/6-311G [kcal/mol]')
    plt.xlim(0, 8)
    plt.ylim(0, 10)

    # Añadir líneas horizontales y verticales
    X = np.linspace(0, 8, 100)
    Y = X + 0.5
    Z = X - 0.5

    plt.plot(X, Y, 'k--', linewidth=1)
    plt.plot(X, X, 'k--', linewidth=1)
    plt.plot(X, Z, 'k--', linewidth=1)
    plt.fill_between(X, Y, Z, color='grey', alpha=0.25)

    plt.grid(color='gray', linestyle='--', linewidth=0.25)

    # Crear el directorio si no existe
    if not os.path.isdir(col):
        os.mkdir(col)
    plt.savefig(f'{col}/graph.png', dpi=600)
    plt.close()  # Cerrar la figura para no sobrecargar la memoria

I obtain:

enter image description here

But I want:

enter image description here

Where each colour of the legend correspond to a different dataset, e.g. Test_1.csv, Test_2.csv and so on...

How can I introduce another dataset into my graphs, generated inside a for-loop?
<p>I am following <a href="https://www.w3schools.com/python/matplotlib_scatter.asp" rel="nofollow noreferrer">this example.</a></p>
<p>I have this code:</p>
<pre><code>import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
df = pd.read_csv('Test_1.csv',header=0,delimiter="\t")
x_col = df[['Ref']]
y_columns = df[['R2SCAN','Ref','TPSS0_D3BJ']]
# Crear las gráficas
for col in y_columns:
plt.figure()
plt.scatter(df["Ref"], df[col])
df = pd.read_csv('Test_2.csv',header=0,delimiter="\t")
# Crear las gráficas
for col in y_columns:
# plt.figure()
plt.scatter(df["Ref"], df[col])
plt.xlabel("DLPNO-CCSD(T)/CBS(3,4) [kcal/mol]")
plt.ylabel(f'{col}/6-311G [kcal/mol]')
plt.xlim(0, 8)
plt.ylim(0, 10)

# Añadir líneas horizontales y verticales
X = np.linspace(0, 8, 100)
Y = X + 0.5
Z = X - 0.5

plt.plot(X, Y, 'k--', linewidth=1)
plt.plot(X, X, 'k--', linewidth=1)
plt.plot(X, Z, 'k--', linewidth=1)
plt.fill_between(X, Y, Z, color='grey', alpha=0.25)

plt.grid(color='gray', linestyle='--', linewidth=0.25)

# Crear el directorio si no existe
if not os.path.isdir(col):
os.mkdir(col)
plt.savefig(f'{col}/graph.png', dpi=600)
plt.close() # Cerrar la figura para no sobrecargar la memoria
</code></pre>
<p>I obtain:</p>
<p><a href="https://i.sstatic.net/AJKbwIN8.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AJKbwIN8.png" alt="enter image description here" /></a></p>
<p>But I want:</p>
<p><a href="https://i.sstatic.net/19heoGy3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/19heoGy3.png" alt="enter image description here" /></a></p>
<p>Where each colour of the legend correspond to a different dataset, e.g. <code>Test_1.csv</code>, <code>Test_2.csv</code> and so on...</p>
<p><strong>How can I introduce another dataset into my graphs, generated inside a for-loop?</strong></p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top