Python Forum
Draw a square-aspect log-linear plot via matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draw a square-aspect log-linear plot via matplotlib
#1
So I have a plot with logarithmic x axis but linear y axis, and I want the plot to be square-aspect, but when I use plt.axis('equal') it does not work and I get the following error:

Error:
UserWarning: aspect is not supported for Axes with xscale=log, yscale=linear 'yscale=%s' % (xscale, yscale))
I searched for it and there are several workarounds (e.g. here) but non of them actually work; like in the aforementioned link neither of ax = gca() or fig = gcf() work. Any ideas how I could make my semi-log plot squared?
Reply
#2
Using numpy (it is a part of pylab/matplotlib) you can do any conversion of the data manually, e.g.:

from pylab import *
x = np.exp(np.linspace(0, 5, 10000))
y = np.cos(x)
ax = fig.add_subplot(111)
fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.plot(np.log(x), y)
ax.set_aspect('auto') #yields square axis ; ax.set_aspect('equal') -- yields rectangular axis 
show()
Can you provide the data or function to be plotted?
Reply
#3
(Jun-19-2018, 07:31 AM)scidam Wrote: Using numpy (it is a part of pylab/matplotlib) you can do any conversion of the data manually, e.g.:

from pylab import *
x = np.exp(np.linspace(0, 5, 10000))
y = np.cos(x)
ax = fig.add_subplot(111)
fig = figure(figsize=(4,4))
ax = fig.add_subplot(111)
ax.plot(np.log(x), y)
ax.set_aspect('auto') #yields square axis ; ax.set_aspect('equal') -- yields rectangular axis 
show()
Can you provide the data or function to be plotted?

Thanks, but I get a lot of warnings with this (I am using Spyder) such as: "name 'fig' is not defined" or "from pylab import *: unable to detect undefined names" and so on! Plus I could not attach my file here Idk why, but regardless the data should not matter, you know whatever plot it is I want the x axis to be in log scale and I want the whole graph to look squared!
Reply
#4
Most change so line 5 where fig is defined comes first,and pylab is no longer recommended.
#from pylab import * # No
import matplotlib.pyplot as plt

x = np.exp(np.linspace(0, 5, 10000))
y = np.cos(x)
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)
#ax = fig.add_subplot(111)
ax.plot(np.log(x), y)
ax.set_aspect('auto') #yields square axis ; ax.set_aspect('equal') -- yields rectangular axis 
show()
Reply
#5
(Jun-19-2018, 05:41 PM)snippsat Wrote: Most change so line 5 where fig is defined comes first,and pylab is no longer recommended.
#from pylab import * # No
import matplotlib.pyplot as plt

x = np.exp(np.linspace(0, 5, 10000))
y = np.cos(x)
fig = plt.figure(figsize=(4,4))
ax = fig.add_subplot(111)
#ax = fig.add_subplot(111)
ax.plot(np.log(x), y)
ax.set_aspect('auto') #yields square axis ; ax.set_aspect('equal') -- yields rectangular axis 
show()

Problem solved! Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pick a line in a plot with matplotlib Romain 3 5,546 Feb-28-2023, 11:56 AM
Last Post: get2sid
  Chi-square with the contingency table applied to the die Leloup 4 1,938 Mar-16-2022, 12:22 AM
Last Post: jefsummers
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,236 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  Matplotlib scatter plot slider with datetime sonny 0 2,919 Feb-05-2021, 02:31 AM
Last Post: sonny
  New to Python & Matplotlib - can I change the plot grid origin to lower left corner? FortyTwo 1 6,097 Aug-22-2020, 08:22 PM
Last Post: matador
  matplotlib creating a scatter plot via PathCollection tpourjalali 0 2,430 Apr-11-2020, 05:59 PM
Last Post: tpourjalali
  How to build linear regression by implementing Gradient Descent using only linear alg PythonSpeaker 1 2,160 Dec-01-2019, 05:35 PM
Last Post: Larz60+
  MatPlotLib 2d plot of current density? ruben 0 2,171 May-13-2019, 06:47 AM
Last Post: ruben
  [pandas]How to liner fit time series data and get linear fit equation and r square Sri 5 3,755 Apr-04-2019, 12:00 PM
Last Post: Sri
  Bar Plot with Python ,matplotlib and sqlite3 tables gauravbhardwajee 0 4,928 Sep-25-2018, 06:17 PM
Last Post: gauravbhardwajee

Forum Jump:

User Panel Messages

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