matplotlib – plt.imshow() of a single color image showing as black

Advertisements I am trying to show a light-gray image using plt.imshow(), but the image turns out black. I tried: import matplotlib.pyplot as plt import numpy as np test_image = np.zeros((3871, 2484)) test_image.fill(200) plt.imshow(test_image, cmap="gray") plt.show() But ended up getting: Matplotlib version: 3.7.1 Numpy version: 1.24.3 Python version: 3.11.3 >Solution : Solution You have to include… Read More matplotlib – plt.imshow() of a single color image showing as black

How to get same color when plotting numpy image array for same pixel value multiple times

Advertisements I am trying to work on some pixel manipulation on a sequence of images. I encountered an issue when plotting same value of pixel having different color when plotted with different range of pixels. For example, I have an image with six different pixel values(0, 2, 3, 4, 5, 6): Code #1 : import… Read More How to get same color when plotting numpy image array for same pixel value multiple times

pandas plot displays wrong months from datetime on x-axis

Advertisements I am trying to plot stacked bar chart using pandas and matplotlib. I want months to be located on x-axis from datetimes. But It always shows "jan", despite the month given. import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd from datetime import datetime df = pd.DataFrame({‘value1’:[1,2], ‘value2’:[5,4]}, index=[datetime(year=2020, month=3, day=3),… Read More pandas plot displays wrong months from datetime on x-axis

Matplotlib does not draw the last line

Advertisements I have a function that takes a list of tuples with float values, from the list I make two new separate lists, each containing the vertex coordinates of the triangle. Then I use matplotlib to draw the vertices. def draw_figure(list_of_floats): x_coord, y_coord = zip(*list_of_floats) x_coord = list(x_coord) y_coord = list(y_coord) # print("Coordinates:") # for… Read More Matplotlib does not draw the last line

ax.get_xlim() doesnt work with datetimeindex

Advertisements ax.get_xlim doesnt provide the right result. When running the code below, I get xmin as 1970-01-01 instead of 2003-01-01. Does anyone know how to fix it? data = pd.DataFrame({ ‘Date’: pd.date_range(‘1980-01-01’, ‘2023-01-01′, freq=’M’), ‘Value’: np.random.rand(516) }) data=data.set_index(‘Date’) data.index=pd.to_datetime(data.index) data=data.resample(‘M’).last() data=data[‘2003-01-01’:] fig, ax = plt.subplots() xmin, xmax = ax.get_xlim() ax.plot(data) print(pd.Timestamp(xmin)) >Solution : First, you… Read More ax.get_xlim() doesnt work with datetimeindex

raise KeyError(key) KeyError: python matplotlib bar chart key error

Advertisements I have below data frame, on which am creating a chart, The expectation is to create as below which is in excel, But while defining the axis in matplotlib, am facing an issue, import matplotlib.pyplot as plt import pandas as pd import random def la_bar(): df1 = pd.DataFrame(lst, columns=[‘source’,’type’,’date’,’count’]) #lst is a data set… Read More raise KeyError(key) KeyError: python matplotlib bar chart key error