পান্ডাস বারপ্লট-এ কীভাবে এক্স-অক্ষের টিক লেবেলগুলি ঘোরান


95

নিম্নলিখিত কোড সহ:

import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75)
plt.xlabel("")

আমি এই প্লটটি তৈরি করেছি:

এখানে চিত্র বর্ণনা লিখুন

আমি কীভাবে এক্স-অক্ষের টিক লেবেলগুলি 0 ডিগ্রি ঘোরান?

আমি এটি যুক্ত করার চেষ্টা করেছি কিন্তু কার্যকর হয়নি:

plt.set_xticklabels(df.index,rotation=90)

উত্তর:


183

rot=0অষ্টিক্সগুলি ঘোরানোর জন্য পরম পাস করুন :

import matplotlib
matplotlib.style.use('ggplot')
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({ 'celltype':["foo","bar","qux","woz"], 's1':[5,9,1,7], 's2':[12,90,13,87]})
df = df[["celltype","s1","s2"]]
df.set_index(["celltype"],inplace=True)
df.plot(kind='bar',alpha=0.75, rot=0)
plt.xlabel("")
plt.show()

ফলন প্লট:

এখানে চিত্র বর্ণনা লিখুন



9

প্রশ্নটি স্পষ্ট তবে শিরোনামটি ঠিক যেমনটি হতে পারে তেমন সঠিক নয়। আমার উত্তরটি হ'ল যারা টিক লেবেলগুলির বিপরীতে অক্ষ লেবেলটি পরিবর্তন করতে চেয়েছিলেন তাদের কাছে , এটিই গৃহীত উত্তর সম্পর্কে। (শিরোনাম এখন সংশোধন করা হয়েছে)।

for ax in plt.gcf().axes:
    plt.sca(ax)
    plt.xlabel(ax.get_xlabel(), rotation=90)


3

নিম্নলিখিতগুলি সহায়ক হতে পারে:

# Valid font size are xx-small, x-small, small, medium, large, x-large, xx-large, larger, smaller, None

plt.xticks(
    rotation=45,
    horizontalalignment='right',
    fontweight='light',
    fontsize='medium',
)

উদাহরণ এবং এপিআই সহ ফাংশন xticks[রেফারেন্স] এখানে

def xticks(ticks=None, labels=None, **kwargs):
    """
    Get or set the current tick locations and labels of the x-axis.

    Call signatures::

        locs, labels = xticks()            # Get locations and labels
        xticks(ticks, [labels], **kwargs)  # Set locations and labels

    Parameters
    ----------
    ticks : array_like
        A list of positions at which ticks should be placed. You can pass an
        empty list to disable xticks.

    labels : array_like, optional
        A list of explicit labels to place at the given *locs*.

    **kwargs
        :class:`.Text` properties can be used to control the appearance of
        the labels.

    Returns
    -------
    locs
        An array of label locations.
    labels
        A list of `.Text` objects.

    Notes
    -----
    Calling this function with no arguments (e.g. ``xticks()``) is the pyplot
    equivalent of calling `~.Axes.get_xticks` and `~.Axes.get_xticklabels` on
    the current axes.
    Calling this function with arguments is the pyplot equivalent of calling
    `~.Axes.set_xticks` and `~.Axes.set_xticklabels` on the current axes.

    Examples
    --------
    Get the current locations and labels:

        >>> locs, labels = xticks()

    Set label locations:

        >>> xticks(np.arange(0, 1, step=0.2))

    Set text labels:

        >>> xticks(np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))

    Set text labels and properties:

        >>> xticks(np.arange(12), calendar.month_name[1:13], rotation=20)

    Disable xticks:

        >>> xticks([])
    """

2

বার গ্রাফের জন্য, আপনি শেষ পর্যন্ত টিকগুলি দেখতে চান এমন কোণটি অন্তর্ভুক্ত করতে পারেন।

আমি rot=0তাদের এক্স অক্ষের সাথে সমান্তরাল করে তুলতে ব্যবহার করছি ।

series.plot.bar(rot=0)
plt.show()
plt.close()

এবং হিস্টোগ্রামগুলির জন্য, এটি খুব অনুরূপ। বা এর rotসাথে প্রতিস্থাপন করুনxrotyrot
— টম
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.