Y-axis ticks are being stacked when plotting functions

51 Views Asked by At

I'm not a Python programmer, but I want to make some charts in Matplotlib. It should be IV Characteristic for my electronic classes at Uni, with X-axis in log scale. I'm able to make good chart without plots.

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

# Sample data
data = data = {
    "IF/R": [
        0, 2.07, 5.08, 10.08, 20.7, 40.7, 50.8, 60.8, 100.9, 200, 
        415, 514, 614, 1015, 2010, 4010, 5190, 6180, 10180, 20100, 
        30100, 40000, 50000
    ],
    "LED RED": [
        0, 1527, 1574, 1607, 1641, 1670, 1678, 1686, 1706, 1732, 
        1760, 1768, 1776, 1797, 1829, 1866, 1881, 1893, 1929, 1984, 
        2020, 2050, 2070
    ]
}
ampers = data["IF/R"]

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

# Set x axis to log
ax.set_xscale('log')

ax.yaxis.set_ticks(range(len(ampers)))
ax.yaxis.set_ticklabels(ampers)

# Add labels and a legend
ax.set_xlabel("X-axis Label")
ax.set_ylabel("Y-axis Label")
ax.set_title("Ampers vs LED RED")
ax.legend()

# Display the plot
plt.show()

enter image description here

But when i want to plot something the Y-axis is going crazy and X-axis is not a power of 10.

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

# Sample data
data = data = {
    "IF/R": [
        0, 2.07, 5.08, 10.08, 20.7, 40.7, 50.8, 60.8, 100.9, 200, 
        415, 514, 614, 1015, 2010, 4010, 5190, 6180, 10180, 20100, 
        30100, 40000, 50000
    ],
    "LED RED": [
        0, 1527, 1574, 1607, 1641, 1670, 1678, 1686, 1706, 1732, 
        1760, 1768, 1776, 1797, 1829, 1866, 1881, 1893, 1929, 1984, 
        2020, 2050, 2070
    ]
}
ampers = data["IF/R"]

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

# Set x axis to log
ax.set_xscale('log')

# Set ampers values as ticks
ax.yaxis.set_ticks(range(len(ampers)))
ax.yaxis.set_ticklabels(ampers)

# Plot first data
plt.plot(data["LED RED"], ampers)

# Add labels and a legend
ax.set_xlabel("X-axis Label")
ax.set_ylabel("Y-axis Label")
ax.set_title("Ampers vs LED RED")
ax.legend()

# Display the plot
plt.show()

enter image description here

I've tried many options of setting ticks to have these exact ampers values but they didn't work. This is the only way I managed to have nice Y-axis, but now there is the problem with plotting. I don't have any idea what causes it.

0

There are 0 best solutions below