logo
down
shadow

matplotlib: default color cycle from style


matplotlib: default color cycle from style

By : Alexandr Krol
Date : November 21 2020, 09:01 AM
it helps some times Is there any way to recover the default color cycle used for plotting lines, which is somehow defined by the matplotlib stylesheet. , You can get the color cycle from the rc-params.
code :
>>> from matplotlib import pyplot as plt
>>> color_cycle = plt.rcParams['axes.color_cycle']
>>> color_cycle
[u'b', u'g', u'r', u'c', u'm', u'y', u'k']


Share : facebook icon twitter icon
How to set the default color cycle for all subplots with matplotlib?

How to set the default color cycle for all subplots with matplotlib?


By : tquest
Date : March 29 2020, 07:55 AM
This might help you Sure! Either specify axes.color_cycle in your .matplotlibrc file or set it at runtime using matplotlib.rcParams or matplotlib.rc.
As an example of the latter:
code :
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

# Set the default color cycle
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["r", "k", "c"]) 

x = np.linspace(0, 20, 100)

fig, axes = plt.subplots(nrows=2)

for i in range(10):
    axes[0].plot(x, i * (x - 10)**2)

for i in range(10):
    axes[1].plot(x, i * np.cos(x))

plt.show()
Get matplotlib color cycle state

Get matplotlib color cycle state


By : RG Prins
Date : March 29 2020, 07:55 AM
I hope this helps . Accessing the color cycle iterator
There's no "user-facing" (a.k.a. "public") method to access the underlying iterator, but you can access it through "private" (by convention) methods. However, you'd can't get the state of an iterator without changing it.
code :
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
color_cycle = ax._get_lines.color_cycle
# or ax._get_lines.prop_cycler on version >= 1.5
# Note that prop_cycler cycles over dicts, so you'll want next(cycle)['color']
import matplotlib.pyplot as plt
import numpy as np

def custom_plot(x, y, **kwargs):
    ax = kwargs.pop('ax', plt.gca())
    base_line, = ax.plot(x, y, **kwargs)
    ax.fill_between(x, 0.9*y, 1.1*y, facecolor=base_line.get_color(), alpha=0.5)

x = np.linspace(0, 1, 10)
custom_plot(x, x)
custom_plot(x, 2*x)
custom_plot(x, -x, color='yellow', lw=3)

plt.show()
Increment matplotlib color cycle

Increment matplotlib color cycle


By : Sagar Rao
Date : March 29 2020, 07:55 AM
This might help you Is there a simple way to increment the matplotlib color cycle without digging into axes internals? , You could call
code :
ax2._get_lines.get_next_color()
ax2.plot([], [])
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = np.random.randint(10, size=10)
y2 = np.random.randint(10, size=10)*100
fig, ax = plt.subplots()
ax.plot(x, y1, label='first')
ax2 = ax.twinx()
ax2._get_lines.get_next_color()
# ax2.plot([], [])
ax2.plot(x,y2, label='second')

handles1, labels1 = ax.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
ax.legend(handles1+handles2, labels1+labels2, loc='best')  

plt.show()
Why does color bars change the color in cycle drawing? (matplotlib)

Why does color bars change the color in cycle drawing? (matplotlib)


By : Hongkang Tian
Date : March 29 2020, 07:55 AM
I hope this helps you . The problem is that you are plotting 10 different bars, all on the same axes. Your bars are being plotted at the same x coordinate and so are plotted on top of each other. This still means that your other bars are still there and so matplotlib cycles the colours of the "new" bars being plotted.
There are 2 things you can do. Firstly you can call plt.cla() which clears the current axes. This however will reset you y axis limits (which makes it look like nothing much is happening). This means you will have to set the y axis limits using ax4.set_ylim(0,20).
code :
import matplotlib.pyplot as plt
from matplotlib import mlab

fig = plt.figure()

ax4 = fig.add_subplot(111)
ax4.set_xlim(-10,80)
ax4.set_ylim(0,30)

def view(ylist):

    xmin = 0
    xmax = 70.0
    dx = 10
    xlist = mlab.frange (xmin, xmax, dx)

    plt.cla()
    ax4.set_ylim(0,20)
    ax4.bar(xlist, ylist, dx)  # can add color="blue" if you don't want to use plt.cla()
    plt.pause(0.5)
    plt.draw()

ylist = [0 for p in range(8)]
for i in range(10):
    view(ylist)
    ylist[0] +=1
    ylist[3] +=2

plt.close()
hist and axvline do not follow color cycle / matplotlib style

hist and axvline do not follow color cycle / matplotlib style


By : user1437480
Date : March 29 2020, 07:55 AM
may help you . I'm trying to plot a distribution and it's mean: , You need to get the next color in the cycle by using
code :
next(ax._get_lines.prop_cycler)['color']
plt.style.use('ggplot')

numbers = np.random.rand(100)
fig, ax = plt.subplots()
ax.hist(numbers)

ax.axvline(numbers.mean(), color = next(ax._get_lines.prop_cycler)['color'])

plt.show()
Related Posts Related Posts :
  • delete rows in numpy array in python
  • What are the possible numpy value format strings?
  • Conditional Selecting of child elements in pdfquery
  • Python: split string by closing bracket and write in new line
  • SyntaxWarning: import * only allowed at module level
  • theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?
  • Django background infinite loop process management
  • How can I use Pandas or Numpy to infer a datatype from a list of values?
  • How to add the sum of cubes using a function in python?
  • django registration redux URL's being effected by url with multiple query parameters
  • python - how can I generate a WAV file with beeps?
  • How can I implement a custom RNN (specifically an ESN) in Tensorflow?
  • Python modulo result differs from wolfram alpha?
  • Django on App Engine Managed VM
  • Python - CSV Reading with dictionary
  • Python script works in librarys examples folder, but not in parent directory
  • Dealing with Nested Loops in Python - Options?
  • Get indices of roughly equal sized chunks
  • python - creating dictionary from excel using specific columns
  • SQLAlchemy Determine If Unique Constraint Exists
  • Can I stop rendering variables in Django?
  • Scrapy: traversing a document
  • Common logger settings in Python logging dictConfig
  • Should I pass the object in or build it in the constructor?
  • 3d and 2d subplots in plotly
  • Apache Spark CombineByKey with list of elements in Python
  • How do I round up to the highest multiple of 10 in python?
  • ValueError: invalid literal for int() with base 10: 'skip'
  • How to get entire VARCHAR(MAX) column with Python pypyodbc
  • Use value of variable rather than keyword in python numpy.savez
  • Overlapping cron job that runs the same Django management command: problematic?
  • Distributed Powerset
  • Set a python variable to a C++ object pointer with boost-python
  • How to change array layout?
  • How do I properly structure Templates and include them in the settings in Django 1.8.6?
  • Function parameter semantics (with nested functions)
  • Not enough arguments for format string python
  • How to extract equation from a polynomial fit?
  • How to enable unicode for Python on Fedora?
  • How to check for a POST method in a ListView in Django views? I'm getting a 405 error
  • Pandas fillna values with numpy array
  • How to change the date format in a list of dates
  • Passing a variable between two test cases?
  • h2o: iterate through rows
  • scipy curve_fit fails on easy linear fit?
  • how to multiply dataframes elementwise and create multi-index -- Python
  • Can Django's development server route mutliple sites? If yes, how?
  • Apply Number formatting to Pandas HTML CSS Styling
  • Python cosine-similarity on all possible pairs in list
  • ImportError: cannot import name Serializer
  • make a shift by index with a pandas dataframe
  • Call Python from C++ OpenCV 3.0
  • How to inherit from GObject class?
  • Prevent from converting string to int in xaxis in plot by groups
  • List sorting in Python (transpose)
  • Different for-"definitions/conditions but same execution code
  • How to control the order of yield in Scrapy
  • Export Pandas data frame with text column containg utf-8 text and URLs to Excel
  • how to understand re.match in python?
  • Django Form Wizard - Setting default value for text input field
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co